Combine two select statement in MySql? - java

I've this table, Have a look there.
create table if not exists thread_question
(
question_id INT NOT NULL auto_increment,
first_name VARCHAR(100) NOT NULL,
last_name VARCHAR(100) NOT NULL,
question_title VARCHAR(500) NOT NULL,
question VARCHAR(100000) NOT NULL,
question_dateTime VARCHAR(100) NOT NULL,
PRIMARY KEY(question_id)
);
create table if not exists thread_answer
(
answer_id INT NOT NULL auto_increment,
question_id INT NOT NULL references thread_question(question_id),
first_name VARCHAR(100) NOT NULL,
last_name VARCHAR(100) NOT NULL,
answer VARCHAR(100000) NOT NULL,
answer_dateTime VARCHAR(100) NOT NULL,
PRIMARY KEY(answer_id)
);
Actually i'm making a forum to show answers, it is working perfect. You could see as well question_id's column is inserted in thread_question as well thread_answer. I wanted to show my answers in one page that how many users has posted there answer.
So i could do that according to this question -> How to show the numbers of answers posted into my forum?
Question
Now I'm trying to combine these two statements:
First statement :
SELECT thread_question.question_id, COALESCE(sub.counts,0) AS NumerOfAnswer
FROM thread_question LEFT JOIN (
SELECT question_id, COUNT(answer_id) AS counts
FROM thread_answer
GROUP BY question_id
) sub ON thread_question.question_id = sub.question_id ORDER BY NumerOfAnswer asc
Second statement :
select * from thread_question
Actually i want to fetch user's first and second name, question and question's title from thread_question's table.
I do use UNION and UNION ALL and SELECT( SELECT..)(SELECT..). But i'm unable to show the result. It's giving me an error everytime.
PLEASE HELP!!
Surely, help would be appreciated!!

Since you already have thread_question table in the query, all you need to do to bring the remaining columns is to add thread_question.*, like this:
SELECT
thread_question.* -- <<== Use .* to bring all fields
, COALESCE(sub.counts,0) AS NumerOfAnswer
FROM thread_question
LEFT JOIN (
SELECT question_id, COUNT(answer_id) AS counts
FROM thread_answer
GROUP BY question_id
) sub ON thread_question.question_id = sub.question_id
ORDER BY NumerOfAnswer asc

Related

Problem with SQL code while using Java DB. Saying parentheses is foreign

I am new to java. I am trying to create two SQL codes to create a table in Java DB (I am using NetBeans 12.5). I set it up how I thought it should be set up but I am still getting an error.
CREATE TABLE CheckInLocation(
CheckInLocationID int NOT NULL PRIMARY KEY,
StationName VARCHAR(30) NOT NULL,
);
CREATE TABLE Passenger (
PassengerID int NOT NULL PRIMARY KEY,
FirstName VARCHAR(30) NOT NULL,
LastName VARCHAR(30) NOT NULL,
CheckInDateTime TIMESTAMP NOT NULL,
FOREIGN KEY (CheckInLocationID) REFERENCES CheckInLocation(CheckInLocationID),
);
The error I am getting is this:
[Exception, Error code 30,000, SQLState 42X01] Syntax error: Encountered ")" at line 7, column 1.
Any help at all I will appreciate. Thank you.
Problem is the final comma in both your create tables. For example, replace
StationName VARCHAR(30) NOT NULL,
with
StationName VARCHAR(30) NOT NULL

How to show the numbers of answers posted into my forum?

i'm making a forum using Java technologies. Actually it is almost near to complete but the problem is I want to show numbers of answer into my forum. Okay let's understand in deeply.
Firstly, i've created a file named as index.jsp where we could see all questions. For e.g Have a look into stackoverflow.com we see all question as well numbers of answers posted in one question. That's all i wanted to show into my index.jsp.
I'm fetching all questions using select * from question_table... Actually into my view question file. I'm fetching the answer using question_id table which is created into answer's table. You know very well to show answer in particular we need to save the same question_id into answer's table as well question's table.
For e.g:
look at this table i designed the same thing:
create table if not exists thread_question(
question_id INT NOT NULL auto_increment,
question_title VARCHAR(500) NOT NULL,
question VARCHAR(100000) NOT NULL,
question_dateTime VARCHAR(100) NOT NULL,
PRIMARY KEY(question_id)
);
create table if not exists thread_answer(
answer_id INT NOT NULL auto_increment,
question_id INT NOT NULL references thread_question(question_id),
answer VARCHAR(100000) NOT NULL,
answer_dateTime VARCHAR(100) NOT NULL,
PRIMARY KEY(answer_id)
);
As well you could see here, question_id is saved into two tables. Same process i'm using here.
Now i want to show my number of answers into my main page. Any idea? what can be used here. I really stuck here. Please help!
Surely, Help would be appreciated!!
EDITED:
Here is my full codes of table:
create table if not exists thread_question(
question_id INT NOT NULL auto_increment,
first_name VARCHAR(100) NOT NULL,
last_name VARCHAR(100) NOT NULL,
question_title VARCHAR(500) NOT NULL,
question VARCHAR(100000) NOT NULL,
question_dateTime VARCHAR(100) NOT NULL,
PRIMARY KEY(question_id)
);
create table if not exists thread_answer(
answer_id INT NOT NULL auto_increment,
question_id INT NOT NULL references thread_question(question_id),
first_name VARCHAR(100) NOT NULL,
last_name VARCHAR(100) NOT NULL,
answer VARCHAR(100000) NOT NULL,
answer_dateTime VARCHAR(100) NOT NULL,
PRIMARY KEY(answer_id)
);
Here is SCREENSHOT.
PLEASE HELP!!
use inner join to fetch number of answer per question
SELECT thread_question.question_id, COALESCE(sub.counts,0) AS NumerOfAnswer
FROM thread_question LEFT JOIN (
SELECT question_id, COUNT(answer_id) AS counts
FROM thread_answer
GROUP BY question_id
) sub ON thread_question.question_id = sub.question_id
ORDER BY NumerOfAnswer
The count of all answers in the entire forum?
SELECT COUNT(answer_id)
FROM thread_answer
Display number of answers in a query that fetches questions for display?
SELECT <...your selected columns...>, COALESCE(a.counts, 0) AS counts
FROM thread_question q
LEFT JOIN (
SELECT question_id, COUNT(answer_id) AS counts
FROM thread_answers
GROUP BY question_id
) a ON q.question_id = a.question_id

How to show user's id table along with another?

I'm making a forum and now I've this table. Please take a look at here:
This table is for users:
create table if not exists login_system(
user_id INT NOT NULL auto_increment,
email VARCHAR(100) NOT NULL,
password VARCHAR(100) NOT NULL,
first_name VARCHAR(100) NOT NULL,
last_name VARCHAR(100) NOT NULL,
gender VARCHAR(50) NOT NULL,
about_yourself VARCHAR(150) NOT NULL,
my_website VARCHAR(100000) NOT NULL,
PRIMARY KEY(user_id)
);
And this table is for questions:
create table if not exists thread_question(
question_id INT NOT NULL auto_increment,
first_name VARCHAR(100) NOT NULL,
last_name VARCHAR(100) NOT NULL,
question_title VARCHAR(500) NOT NULL,
question VARCHAR(100000) NOT NULL,
question_dateTime VARCHAR(100) NOT NULL,
PRIMARY KEY(question_id)
);
I want to show a user's link If he/she posts his/her question. It must suppose to provide a link after hover on name of the persons. So that other peoples could see his/her profile information.
I can simply fetch some questions to show into forum using this:
<%
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/forum", "root", "1234");
Statement st=con.createStatement();
ResultSet rs=st.executeQuery("select * from thread_question where question_id="+question_id);
while(rs.next()){
String first_name=rs.getString("first_name");
String last_name=rs.getString("last_name");
String thread_title=rs.getString("question_title");
String thread_question=rs.getString("question");
String thread_dateTime=rs.getString("question_dateTime");
...
...
...
%>
Now my wish is to show login_system's table along with question's table you know pretty well that we couldn't create two statements along here. So please help.. I want to get user_id from login_system table where i could easily show his/her profile by clicking the link. Can you please help me? How to do that?
First follow a horse_with_no_name's adviece an do NOT put SQL code into your JSP pages for security reasons.
Also please fix your grammar/spelling as it was really hard to read your question.
Another thing is you are going to want to go read about normalizing database structures http://en.wikipedia.org/wiki/Database_normalization
I know pretty well that you can make more than one sql statement if you would like to, it's just another connection, but in this case you should not need to (And most cases)
Change your thread_question table to this
create table if not exists thread_question(
question_id INT NOT NULL auto_increment,
user_id id INT NOT NULL,
first_name VARCHAR(100) NOT NULL,
last_name VARCHAR(100) NOT NULL,
question_title VARCHAR(500) NOT NULL,
question VARCHAR(100000) NOT NULL,
question_dateTime VARCHAR(100) NOT NULL,
PRIMARY KEY(question_id)
);
It looks like the SQL you want is something like this?
SELECT
LS.FIRST_NAME,
LS.LAST_NAME,
TQ.question_title,
TQ.question,
TQ.question_dateTime
FROM
login_system as LS
JOIN thread_question as TQ ON
LS.user_id = TQ.user_id
There are various issues with your approach. First, as commented, you should not put the connection code directly into the JSP. Second, you should not create the statement by concatenating strings because of the risk of SQL injection. Make sure you understand what that means and use a PreparedStatement instead.
To finally answer your question, rather than having first and last name in the thread_question table, you should have a foreign key to the id in the login_system table and then use a join in your SQL statement. I guess it would not help you a lot if I would paste the verbatim solution here.
Follow Shifty solution and if you don't want to join the table then there is one more solution i.e
1. Fire query to you your thread_question and take data.
2. When you are preparing your page and giving userLink you have to attach hover event in javascript which take userid as input and fire ajax call to take the user related data and use this data.

compare values from two tables

I have 2 tables. First one holds the total values of some shopping lists and the second table holds the products in that list. When a shopping list is done the total value is added into the total table together with some informations like the list number(nrList which is some kind of list id) and the number of products on that list nrProducts while the products go into the listproducts table.Lets say there are 3 products tomato,oranges and apples.They will all share the same nrList which,as mentioned before,is something like the list id.
First table totals:
CREATE TABLE IF NOT EXISTS `totals` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`nrList` int(11) NOT NULL,
`nrProducts` int(11) DEFAULT NULL,
`total` double NOT NULL,
`data` date DEFAULT NULL,
`ora` time DEFAULT NULL,
`dataora` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`Operator` varchar(50) DEFAULT NULL,
`anulat` tinyint(1) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
UNIQUE KEY `id` (`id`)
)
Second table listproducts:
CREATE TABLE IF NOT EXISTS `listproducts` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`nrList` int(11) DEFAULT NULL,
`product` varchar(50) DEFAULT NULL,
`quantity` double DEFAULT NULL,
`price` double DEFAULT NULL,
`data` date DEFAULT NULL,
`operator` varchar(50) NOT NULL,
`anulat` tinyint(1) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`)
)
Now,i have two things i want to do,they are very similar.
Lets say i have a list with 3 products.In the totals table there will be a row with some info and with the total=10$ nrProducts=3 and nrList=1.In the listproducts table i will have 3 rows all having nrList=1 and each having price=3$,3$,4$.
Now,i want the check the following :
1.That if the value of nrProducts=3 then i have products for that list in the other table.
2.Check if the total in the first table is equal to the sum of the products in the second table.(quantity*price SUM)
I've done some stuff but i don't know what to do next.
I managed to get the number of products for each list from the second table by using this:
SELECT nrList,operator,COUNT(*) as count FROM listproducts GROUP BY nrList
But i don't know how to compare if the values are equal without doing two queries.
For the second thing again, I know how to get the sum but i don't know how to compare them without doing two separate queries.
SELECT SUM(price*quantity) FROM `listproducts` WHERE nrList='10' and operator like '%x%'
I can also do something like what i've done in the other select,this is not the issue.
The issue is that i don't know how to do the things i want in a single select instead of doing two and comparing them.I'm doing this in java so i can compare but i'd like to know if and how i can do this in a single query.
Thanks and sorry for the long post.
You can try something like this:
SELECT totals.nrList,
IF (totals.nrProducts = t.nrProductsActual, 'yes', 'no') AS matchNrProducts,
IF (totals.total = t.totalActual, 'yes', 'no') AS matchTotal
FROM totals INNER JOIN
(SELECT nrList,
COUNT(*) AS nrProductsActual,
SUM(quantity*price) AS totalActual
FROM listproducts
GROUP BY nrList) AS t ON totals.nrList = t.nrList

Mahout and custom table for data model

The documentation says that the table of ratings should look like this:
CREATE TABLE taste_preferences (
user_id BIGINT NOT NULL,
item_id BIGINT NOT NULL,
preference REAL NOT NULL,
PRIMARY KEY (user_id, item_id)
);
However, in my implementation table of ratings is as follows:
CREATE TABLE taste_preferences (
profile_id BIGINT NOT NULL,
event_id BIGINT NOT NULL,
status_id BIGINT NOT NULL,
PRIMARY KEY (user_id, item_id)
);
Where the grade is in the form status_id (go, no go, maybe I'll go).
The users table as follows:
CREATE TABLE user (
profile_id_1 BIGINT NOT NULL,
profile_id_2 BIGINT NOT NULL,
profile_id_3 BIGINT NOT NULL,
...
);
A user can have multiple profiles, I need to compare these data to users.
I need to write its own implementation of data model? Which way do I see, that would solve this problem? Thanks!
You don't have rating data here, in any form. So, you can't use ratings in recommendations. That's fine; you just have "boolean" data.
Or, you're saying you need to use status but I'm not clear how you want to use it.
You can certainly use your taste_preferences table. Just use MySQLBooleanPrefJDBCDataModel or similar. The user table is irrelevant.

Categories

Resources