compare values from two tables - java

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

Related

Combine two select statement in MySql?

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

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

#1467 - Failed to read auto-increment value from storage engine

I was trying to create a new object and this error appeared:
java.sql.sqlexception failed to read auto-increment value from storage engine
So I went to the phpMyAdmin to create the object there and the same showed up:
MySQL said: Documentation
1467 - Failed to read auto-increment value from storage engine
then I clicked on edit, and it was there:
INSERT INTO `reservation`.`room` (`idroom`, `number`, `floor`, `description`, `characteristics`, `cost`, `status`, `type`) VALUES (NULL, '114', '3', 'ss', 'ss', '550.00', 'Available', 'ss')
(idroom is supposed to be auto-incremented.)
I already read other posts where they say I have to put this:
ALTER TABLE `table_name` AUTO_INCREMENT = 1
but I have no idea where to put that. Is there a better solution?
Your INSERT statement is wrong. Since idroom is AUTO_INCREMENT; you must not include it in the column list on your insert command. Your insert statement should look like below. Notice that I have removed idroom column from insert column list and not passing NULL as well in value list.
INSERT INTO `reservation`.`room` (`number`, `floor`, `description`,
`characteristics`, `cost`, `status`, `type`)
VALUES ('114', '3', 'ss', 'ss', '550.00', 'Available', 'ss')
I also struggled with this problem and searched, and didn't find anything. Then the following worked for me; I guess it might work for your problem. Thx.
1st:
-delete (before backup)->all data from your database.
-try to run your Java program again, or any program you want.
If it fails then go to 2nd.
2nd:
- backup all data from your table
- delete table completely
- create table again; example shown below:
CREATE TABLE `users` (
`id` int(6) NOT NULL,
`f_name` varchar(30) NOT NULL,
`l_name` varchar(30) NOT NULL,
`address` varchar(50) DEFAULT NULL,
`phone_num` varchar(12) DEFAULT NULL,
`email` varchar(30) DEFAULT NULL
);
ALTER TABLE `users`
ADD PRIMARY KEY (`id`);
AUTO_INCREMENT for table `users`
ALTER TABLE `users`
MODIFY `id` int(6) NOT NULL AUTO_INCREMENT;

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.

MySQL field type

CREATE TABLE IF NOT EXISTS `user` (
`USER_ID` bigint(20) NOT NULL auto_increment,
`USER_ABOUT_YOU` varchar(255) default NULL,
`USER_COMMUNITY` tinyblob,
`USER_COUNTRY` varchar(255) default NULL,
`USER_GENDER` varchar(255) default NULL,
`USER_MAILING_LIST` bit(1) default NULL,
`USER_NAME` varchar(255) default NULL,
`USER_PASSWORD` varchar(255) default NULL,
PRIMARY KEY (`USER_ID`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
Why is the USER_COMMUNITY defined as tiny blob. This field accepts a value of checkbox. When i change it to some other datatype i get an error? Why is to so?
http://www.vaannila.com/spring/spring-hibernate-integration-1.html
That's how it is defined:
#Column(name="USER_COMMUNITY")
public String[] getCommunity() {
return community;
}
public void setCommunity(String[] community) {
this.community = community;
}
The table doesn't store the checks but an array of Strings. And it looks like, TINYBLOB is the correct datatype on MYSQL for storing arrays.
There is no 'value of checkbox'. A checkbox returns a string as do most other HTML input controls. These checkboxes actually return a list of values, because they all have the same name. I don't know how this value is stored actually, but I can imagine that is is stored as an array of string with some accompanieing meta data. This kind of data will be hard to store in another field type.
In what field type did you want to change it, and what was the error you got?
I would actually not store this data this way, but give user a detail table instead in which you can store the communities the user is a member of. But this tutorial seems to focus more on jsp than database normalisation. ;)

Categories

Resources