mysql query of 3 tables [closed] - mysql

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 8 years ago.
Improve this question
I have 3 tables as follow:
table "first"
---------------
id item
1 pen
2 book
table "second"
------------------
id color
1 A
2 B
3 C
table "third"
------------------------
id first second
1 1 2
2 2 2
3 2 3
Table "third" has relationship between the "second" and "first"
I would like to query from "first" table for the "item" book which has ID=2 an check it in "third" table and get third.second column value that matches third.first=2 and then pull that values(which could be multiple) and get second.color values from the table "second"
I hope have clearly stated my question.
I have tried this but it gives error:
#1242 - Subquery returns more than 1 row
SELECT color FROM `second` WHERE `id`=
(SELECT second FROM `third` WHERE `first`=
(SELECT id FROM `first` WHERE `item`='book'))

You need JOIN syntax, like this:
SELECT
second.color
FROM
first
LEFT JOIN third
ON first.ID=third.first
LEFT JOIN second
ON third.second=second.ID
WHERE
first.item='book'
AND
second.ID IS NOT NULL
for better understanding JOIN, read this article.

Related

sql statement to select all from another table [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
lists table
id user_id list_name
1 1 test
2 1 test2
items table
id list_id item_name price_item item_checked
1 1 apple 2 0
2 2 orange 2 0
result should be
id item_name price_item item_checked
1 Apple 2 0
How would i accomplish to select all from list 1
Your question is not clear but I will try my best to answer it.
To get the results you have posted:
select *
from Items_Table
Title says select all from a different table. I assume lists is the first table and items is the "different" table you are referring to.
Select Items.*
from Lists_Table Lists
left join Items_Table Items on Items.list_id = Lists.id
Now, lets assume you were looking for specific items, say where Id is 1 in the lists table. Then you would have the following:
Select Items.*
from Lists_Table Lists
left join Items_Table Items on Items.list_id = Lists.id
where Lists.id = 1
You can replace Lists.id in the where statement with any other column and set it equal to value you are looking for.
I hope this was helpful. Please try to be a bit more clear about what you are looking for!
Good Luck!

Mysql query - grouping results [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I have 2 tables and basically what I like to do is group the results or counts together for display. Tried different version of mysql statement but not getting anywhere.
The 2 example tables are:
tbl_One
index O_priority
1 low
2 medium
3 high
tbl_Two
t_priority
2
1
3
3
2
3
1
1
1
expected results:
low = 4
medium = 2
high = 3
SELECT T1.O_priority,T2.c FROM tbl_One as T1 LEFT JOIN (SELECT count(*) as c,t_priority FROM tbl_Two GROUP BY t_priority) as T2 ON T1.index = T2.t_priority;
Join the tables, then group the results:
SELECT tbl_One.O_priority, COUNT(*)
FROM tbl_One JOIN tbl_Two ON tbl_Two.t_priority = tbl_One.index
GROUP BY tbl_One.index
See it on sqlfiddle.
Simple as much as you can, try this:
SELECT count(o.index) as `index`, o.O_priority
FROM tbl_One o join tbl_two t on t.t_priority = o.index
group by t.t_priority;
SQL Fiddle

SQL Query group shuffle [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 8 years ago.
Improve this question
I have a query like
select id, item, producer from table
The result is like this:
id item producer
1 apple A
2 pear A
3 peach A
4 orange B
5 strawberry B
6 melon B
I want to shuffle this result (order by id DESC) and get something like this
item producer
strawberry B
pear A
orange B
apple A
peach A
melon B
I DON'T want to display like this:
ALL A ITEM
ALL B ITEM
ALL C ITEM...
Use the rand function in ORDER BY like this:
select id, item, producer from table order by rand();
To Shuffle the selection you can use rand()
The Answer for the link below contains more information.
SELECT id, item, producer
FROM table
ORDER BY RAND()
MySQL: Shuffle a limited query result?
select id, item, producer from table order by rand()
Use Order BY rand() to randomly order the results

Replace multiple value in one field and value from other table [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Hello i have two table
table player
id Player Position
1 Messi 1,2,4
2 C.Ronaldo 1,2,3
3 Neymar 2,3
table position
id pos
1 CF
2 ST
3 WF
4 MF
and i want output like this
id player pos
1 Messi CF,ST,MF
2 C.Ronaldo CF,ST,WF
3 Neymar ST,WF
my sql fidle http://sqlfiddle.com/#!2/bf206/1
You really shouldn't use a comma separated field like this. Hard to read, inefficient and will cause major problems in the future.
However it is possible to do what you want like this, if the order of the positions for a player are not important:-
SELECT a.id, a.Player, GROUP_CONCAT(b.pos)
FROM player a
INNER JOIN position b
ON FIND_IN_SET(b.id, a.position) > 0
GROUP BY a.id, a.player
To keep the order you could try this (not tested):-
SELECT a.id, a.Player, GROUP_CONCAT(b.pos ORDER BY FIND_IN_SET(b.id, a.position))
FROM player a
INNER JOIN position b
ON FIND_IN_SET(b.id, a.position) > 0
GROUP BY a.id, a.player

Executing "simple" sql query [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 9 years ago.
Improve this question
Here is data in my table :
id name surname place
1 test1 isdad YES
2 test2 spreda YES
3 test3 me NO
4 test4 smallvile YES
What I'd like to get, all ids where place='YES'. So Ids are
1
2
4
and the result count is 3. I want to append that number to the end of the results, so the result should look like this:
1
2
4
3
There is almost definitely a better way to do this. That said, you could do:
SELECT id FROM myTable WHERE place = 'YES'
UNION ALL
SELECT COUNT(*) FROM myTable WHERE place = 'YES'
UNION ALL will append the result number as a new row rather than attempting to add it as another column.
Please don't do this; the results will be mixed, it stands in stalwart defiance of good software design and will be baffling to anyone who comes along later and has to work on this piece of code.
In MySQL, you can do something similar to this in a single query using WITH ROLLUP:
select id, count(*)
from myTable
where place='YES'
group by id
with rollup
SELECT id FROM TABLE WHERE place='YES'
will return with the ids what you want, and the rowcount will store the count number, if you use this with php or c++/c#