Return multiple rows from subquery - mysql

Basically I have two tables. 1 and 2. I need the field2 column in the table2 table to return multiple rows. I tried the below join (simplified the columns) but unfortunately it returns me only one result.
SELECT table1.field1, table1.field2, table1.field3, sub_q.field4
FROM table1
JOIN (
SELECT t2.field4, t2.filter1, t2.filter2 FROM table2 t2
) sub_q ON (sub_q.filter1 = table1.id AND sub_q.filter2 = 1)
##Should return multiple rows
##but returns only one!
WHERE table1.id = ..;
Edit:
I created a schema here: http://sqlfiddle.com/#!9/1c5737 with the select query as
SELECT t1.field1, t1.field2, t1.field3, t2.field1
FROM table1 t1
JOIN table2 t2 ON t2.filter1 = t1.id AND t2.filter2 = 1
WHERE t1.id = 1;
Only to find out that it works there, so I come back in shame to accept the answer and check where I messed up in my query (probably one of the fields)

Why are you using a subquery in the join? This is how it should be written:
SELECT table1.field1, table1.field2, table1.field3, t2.field1
FROM table1 t1
JOIN table2 t2 ON t2.filter1 = table1.id AND t2.filter2 = 1
Also it is likely that you need LEFT JOIN (or INNER JOIN) instead of JOIN, but cannot be sure without more details on what you're trying to achieve.

Related

SQL query gives no results when one of the tables is empty

I'm trying to perform a SQL query like this:
SELECT
t1.*
FROM
`table1` t1,
`table2` t2
WHERE
t1.x = 1
and the table called table2 (t2) is empty but inside t1 there are entries.
For example this query works perfect:
SELECT
t1.*
FROM
`table1` t1
WHERE
t1.x = 1
So just by adding the second table t2 in the FROM part the query gives no results anymore. So I don't understand what is going on here. In my case it should be possible that one of the tables is empty but the query still needs to give results. How can I achieve this?
Your syntax is using an implied JOIN between table1 and table2, and could be rewritten as:
SELECT t1.*
FROM `table1` t1,
CROSS JOIN `table2` t2
WHERE t1.x = 1
This is JOINing everything in table1 against everything in table2. However, as table2 is empty there is nothing to join to.
With an implied CROSS JOIN, results are only returned when the row on both sides of the join is matched, which in this case it cannot be. Therefore, nothing is returned.
If you rewrote the statement to use a LEFT JOIN, you would see all results from table1, and only matching results from table2:
SELECT t1.*
FROM `table1` t1,
LEFT JOIN `table2` t2 ON 1 = 1
WHERE t1.x = 1
Incidentally, typically you would only use this kind of query if there is a relationship in the data between table1 and table2. In this case, you would JOIN on the related columns, like so:
SELECT t1.*
FROM `table1` t1,
LEFT JOIN `table2` t2 ON t2.matchedColumn = t1.matchedColumn
WHERE t1.x = 1

Pull Data from 3 mySQL Tables

I have a query that selects all the data from one table and specific columns from another where two columns are equal and another column equals a specific value.
SELECT table1.*, table2.column1, table2.column2 FROM table1
INNER JOIN table2 ON table1.column3=table2.column3
WHERE table1.column1='foo';
Is it possible to pull table3.column1 & table3.column2 from a table3 where table3.column3=table1.column3 AND table3.column4='bar' ?
The thing that makes it more complex is that the data in table3 is optional and may not always exist; however I still want the query to return table1 & table2's data but with table3.column1 & table3.column2 just being presented as NULL or EMPTY...
Im struggling to get my head around it myself, and any insight or assistance would be greatly appreciated.
Use left join:
SELECT table1.*, table2.column1, table2.column2, table3.column1, table3.column2
FROM table1
INNER JOIN table2
ON table1.column3 = table2.column3
LEFT JOIN table3
ON table3.column3 = table1.column3 AND table3.column4 = 'bar'
WHERE table1.column1 = 'foo';
When there is no corresponding record in table3 table3.column1 and table3.column2 will be null
And if column1 and column2 names are not unique column names in select clause you need to give them an alias
select t1.col1, t2.col2, t3.col3
from table1 as t1
inner join table2 as t2 on t2.col3.t1.col2
left join table3 as t3 on t3.col3.t2.col2
where t1.col1 = 'test';
it can be done like this.

How to combine 2 columns from 2 tables and subtract duplicates

I have 2 tables t1 and t2. Each have a customer ID column. What I am looking for is to join the 2 columns and SUBTRACT the duplicates.
My EG:
Table1 and Table2 with the IDs for each
I have tried a union query. The result I am left with is ID = 1,2,3,4,5,6,7,8,9,10. Where, what I'm after is subtracting 1-5 from Table2 and the result = 6,7,8,9,10.
I hope that makes sense and that someone is able to help. Sorry if this is a bit too simple compared to what you're all used to.
In SQL Server you can use the EXCEPT operator:
select ID
from Table2
except
select ID
from Table1
Mysql does not support it though. Using a an in clause or a left join would work in both servers:
--Using In clause
SELECT ID
FROM Table2
WHERE ID NOT IN
(
SELECT ID
FROM Table1
);
--Using join
SELECT Table2.ID
FROM Table2
left join Table1
on Table2.ID = Table1.ID
where Table1.ID is null
Use left outer join
select * from t1 left outer join t2 on t1.customerid = t2.customerid

how to display two table records (selected column)

I created two tables:
Name:table1,table2
table1 consists of:id,name,contactnumber
101,john,9955443322
102,peter,9955443311
table2 consists of:id,place,date
101,chennai,15-05-2014
102,munbai,13-05-2014
select table1.id
,table1.contactnumber
,table2.date
from table1,table2
where table2.date = 29-09-2014
&& table2.loannumbers=table1.loannumber
But returned empty result set.
I want to display columns:
id,name,date
I want to display rows :
(table2)date=15-05-2014 and (table1)id=id(table2).
Try this
SELECT table1.id, table1.name, table2.date
FROM table1
INNER JOIN table2 ON table1.id = table2.id;
First of all, don't use this syntax for jointures between tables. This is an old school notation and using explicit jointures will be much more readable.
Here is the query you're looking for:
SELECT T1.id
,T1.name
,T2.date
FROM table1 T1
INNER JOIN table2 T2 ON T2.id = T1.id
AND T2.date = '2014-05-15'
Hope this will help.

How to join only one column?

SELECT * FROM table1
LEFT JOIN table2
ON table1.id = table2.table1_id
WHERE table1.id = 1
I need to join only one column from table 2, say first_name.
How can I do that?
Assuming that you mean "select one column from table 2":
SELECT table1.*, table2.first_name
FROM table1
LEFT JOIN table2
...
The accepted answer is the correct answer but I have encountered a strange error when the tables are in two different databases:
Assuming that table1 is in database1 and table2 is in database2.
Initially I have tried this:
SELECT *, database2.table2.first_name
FROM table1
LEFT JOIN database2.table2
ON database1.table1.id = database2.table2.table1_id
WHERE table1.id = 1
The strange thing is that if I try this query from PHP PDO there were no errors but the result contained all columns from database2.table2 (expected only first_name column).
But if I have tried the same query from phpmyadmin got a sintax error:
Table 'database2.table1' doesn't exist
So, for solve that, then all databases need to be specified implicitly like this:
SELECT database1.table1.*, database2.table2.first_name
FROM database1.table1
LEFT JOIN database2.table2
ON database1.table1.id = database2.table2.table1_id
WHERE database1.table1.id = 1
Take your original code and substitute * with table1.*, table2.YourChosenColumn
SELECT table1.*, table2.YourChosenColumn
FROM table1 LEFT JOIN table2
ON table1.id = table2.table1_id
WHERE table1.id = 1
Do you mean in addition to your already stated query:
SELECT * FROM table1
LEFT JOIN table2
ON table1.id = table2.table1_id
WHERE table1.id = 1 and table1.first_name = table2.first_name