2 table query - select from each and report from both - mysql

My platform: Ubuntu 12.04
I have in my database:
- table1: box, tray, ...
- table2: tray, bolt, ...
I've tried this sql query:
select `tray`,bolt from table2 where `tray` in ( select `tray` from table1 where `box` > 11 );
gives me a list of trays and bolts.
I would like to have: box, tray and bolts as output. How?
thanks in advance. A pointer to a good tutorial for extra credit? :-)

You should use an inner join instead of IN() :
SELECT t.tray,t.bolt,s.box
FROM table2 t
INNER JOIN table1 s
ON(t.tray = s.tray and s.box > 11)
As #Reto mentioned, you can read about joins here!
In general: to get data from more then 1 table , you have to either use JOIN, or use a sub query in the select.
A solution with a sub query(correlated query) :
SELECT t.box,t.tray,
(select s.bolt from table2 s where s.tray = t.tray) as bolt
FROM table1 t
WHERE t.box > 11)

You are looking for a JOIN:
select t1.box, tray, t2.bolt
from table1 t1 join
table2 t2
using (tray)
where t1.box > 11;

Related

SQL select data from multiple tables with subqueries (including data from inner join) Error: 1242

I want to display the ID from Table1 (TID) and the results of an inner join.
The following statement is not working.
Situation: Two Tables:
Table 1 PK:TID, FK: Table2_PID
Table2 PK: PID, Name
Among other data I want to display the Name of every PID in Table1 which is stored in Table2.
SELECT T.TID
,(Select P.Name
from mydb.Table2 P
inner join mydb.Table1 T
on P.PID=T.Table2_PID)
FROM mydb.Ticket T;
Result: Error Code 1242. Subquery returns more than 1 row
I do know that the result returns more than 1 row, but I want to show the Name of every PID in Table1 which is stored in Table2. So any ideas on how I can do that?
PS: I'm using mySQL and working with MySQL Workbench v6.3
You don't need use the inner joins for getting all the names of the ID. You can try the default join to achieve the result.
select t2.pid, t2.name from mydb.Table2 t2, mydb.Table1 t1 where t1.pid = t2.pid;
Hope this helps.
You must use join like this
select t1.TID,t2.Name from Table1 t1 left join Table2 t2 on t1.Table2_PID=t2.PID
Thanks for the response, but the question/problem still remains.
It wasn't about the join.
It is about the subquery and selecting multiple rows within it.
Thank you guys,
I was thinking of a solution way to complicated. I resolved it using a simple where statement.
SELECT T.TID, P.Name
FROM mydb.table1 T, mydb.table2 P
WHERE P.PID=T.table2_PID;

Joins with a SELECT command

I have been looking and trying to join two tables to get one result but with no luck. (yes a newbie)
Here is the scenario:
Table T1
ID | t1_name | **t1_value**
Table T2
ID | t2_something | **t2_name** | t2_more | t2_etc
What I am trying to do is a SELECT query to get a result to ECHO the t1_value.
This is from t2_name equaling the same as t1_name then return the t1_value to ECHO.
These are based on each indivdual "t2 ID" (100 + ids) having its own result.
Im sure this is an easy solution, and all the reading and research I have done on joins i just cant seem to get my head around it. Maybe over reading....
If you can offer a solution/help, (with how it works so I can learn for next time)
That would be appreciated.
JOIN the two tables with the condition t1.t2_name = t2.t2_name, then in the SELECT clause select whatever you want to select.
Something like:
SELECT
t2.ID,
t1.t1_value,
...
FROM t1
INNER JOIN t2 ON t1.t1_name = t2.t2_name;
You might also need to have a look at the different types of JOIN, see this for more information:
A Visual Explanation of SQL Joins.
Try this query
select t1.t1_value
from t1
inner join t2 on t1.t1_name=t2_name
For more study you can refer INNER JOIN
And Different type of JOIN
A VISUAL REPRESENTATION OF JOIN
Select t1_value from T1, T2 where T1.t1_name = T2.t2_name
SELECT t1_name.t1, t2_name.t2 FROM t1, t2 WHERE t1.t1_name = t2.t2_name
That should work as expected, just to clarify you can also use INNER JOIN syntax as referenced here : http://www.tizag.com/mysqlTutorial/mysqlleftjoin.php
You can achieve by just joining two tables t1 and t2.
SELECT
t1.Name,
t1.value
FROM t1,t2
WHERE t1.t1_name = t2.t2_name

Select both ID columns when using UNION and GROUP BY

I'm desperate with this query. I have two tables table1 and table2, tables are identical but they have different data. I'm trying to remove duplicities by columns code and manufacturer. To do that I need in final result ID from table1 ID from table2 and also columns code and manufacturer
SELECT * FROM (
SELECT id,code,manufacturer FROM table1 WHERE manufacturer = 1
UNION SELECT id,code,manufacturer FROM table2 WHERE manufacturer = 1
) AS t GROUP BY code HAVING COUNT(*) > 1
But in result i got only values from table1. It's OK but I just need to get there id from table2 too. Please can anyone give me some tips how to do this ?
You have two basic problems:
Problem 1:
You are using UNION when you should be using UNION ALL, because UNION removes duplicates!
Problem 2:
This isn't the right way to go about the problem. You should be using a simple join, not a union.
Try this:
SELECT
t1.id as table1_id,
t2.id as table2_id,
t1.code,
t1.manufacturer
FROM table1 t1
JOIN table2 t2
ON t2.code = t1.code
AND t2.manufacturer = t1.manufacturer
WHERE manufacturer = 1 -- this WHERE clause is optional
Your use of the WHERE clause is a little odd - consider removing it to get all duplicates from all manufacturers.

Debug on subselection / LEFT JOIN?

I'm working on this query on mySQL - it seems to be processed on a website with PEAR and MDB2 on the running server (dont know why… din't do that myself). On my local test system it generates always MDB2 error. PHPmyadmin doesn't do this query as well.
The Subselection is needed because there are not just one but four subselects in this query.
SELECT * FROM table1
WHERE table1.orderID
IN
(
SELECT *
FROM table1
LEFT JOIN table2
ON (table1.customID = table2.customID)
WHERE table1.active=1
)
I can simplify it like that (works):
SELECT * FROM table1
WHERE table1.orderID
IN (1,2,3)
The Subselect works, too:
SELECT *
FROM table1
LEFT JOIN table2
ON (table1.customID = table2.customID)
WHERE table1.active=1
Thank you so much for any help!
The inner query should return exactly one column, like:
SELECT *
FROM table1
WHERE table1.orderID IN
(
SELECT orderId
-- ^^ here
FROM table1
LEFT JOIN
table2
ON table1.customID = table2.customID
WHERE table1.active=1
)

How to get data from 2 mysql tables

what is the syntax if I want to utilize 2 or more tables for my mysql query.
Example, I'm going to fetch the idnumber from the 1st table and the religion on the 2nd table. And the query will return the combined version of those 2 tables showing only the religion and idnumber.
The code might look something like this , but it doesn't work:
select t1.IDNO, t1.LNAME t2.RELIGION from t1, t2 where t2.IDNO='03A57'
The SQL query would be as follows:
SELECT a.idnumber, b.religion FROM table1 a, table2 b
You can add conditions from both tables as well by doing the following:
SELECT a.idnumber, b.religion FROM table1 a, table2 b WHERE b.religion = 'Christian'
More information can be found in this thread: http://www.astahost.com/info.php/mysql-multiple-tables_t12815.html
SELECT t1.IDNO, t1.LNAME FROM t1 LEFT JOIN t2.RELIGION ON ( t2.IDNO = t1.IDNO )
(more or less)
The Join is the command that will link the two.
http://dev.mysql.com/doc/refman/5.0/en/join.html
The code below would do a cross-join.
SELECT tb1.id, tb2.religion FROM tb1 JOIN tb2 ON (tb1.religion_id = tb2.religion_id) WHERE t2.IDNO='03A57'
Again... see http://dev.mysql.com/doc/refman/5.0/en/join.html...