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.
Related
SELECT Table1.Filter, Table1.Condition, Combined.Data FROM Table1
LEFT JOIN
(SELECT Key, Data FROM IF(Table1.Filter, Table2, Table3))) AS Combined
ON Table1.Condition = Combined.Key
I want to create a MySQL View that shows all columns of Table1, and a column from either Table2 or Table3 depending on the field on Table1.Filter.
One simple solution is to LEFT JOIN both Table2 and Table3, with NULL on the column that is not applicable. Is there a way to avoid creating 2 columns?
I cannot UNION Table2 and Table3 as they might contain the same Key.
The following should do what you want:
SELECT t1.Filter, t1.Condition,
COALESCE(t2.Data, t3.Data) as Data
FROM Table1 t1 LEFT JOIN
Table2 t2
ON t1.Filter AND t2.Key = t1.Condition LEFT JOIN
Table3 t3
ON (NOT t1.Filter) AND t3.key = t1.condition;
You cannot have conditionals choosing tables in the FROM. But, you can have conditions in the ON conditions.
consider my scenario like i have 3 tables
table1,
table2,
table3 i want to fetch some colums from table2 or table3 on the basis of some condition
like
select tb1.*,
if(tb1.status='true' then tb2.name else tb3.name)
from table1 tb1, table2 tb2, table3 tb3
where tb1.aid=tb2.aid and tb1.aid=tb2.aid
in short i want to display some column from either table2 or table3 on the basis of condition
You can use CASE EXPRESSION :
SELECT tb1.*,
CASE WHEN tb1.status = 'true' THEN tb2.name ELSE tb3.name END as `name`
FROM table1 tb1
INNER JOIN table2 tb2
ON(t1.aid = tb2.aid)
INNER JOIN table3 tb3
ON(tb1.aid = tb3.aid)
Or with IF() like you wanted :
SELECT tb1.*,
IF(tb1.status='true' ,tb2.col1,tb3.col1) as col1,
IF(tb1.status='true' ,tb2.col2,tb3.col2) as col2,
IF(tb1.status='true' ,tb2.col3,tb3.col3) as col3
.....
Also, try to avoid the use of implicit join syntax(comma separated) and use the proper syntax of a join, this will help you avoid mistakes like the one you did (compared both conditions to tb2 instead of one to tb2 and one to tb3
if(tb1.status='true',tb2.name,tb3.name) as name
SELECT
tb1.*
, CASE
WHEN tb1.status = 'true' THEN tb2.name
ELSE tb3.name
END AS var_name
FROM table1 tb1
INNER JOIN table2 tb2 ON tb1.aid = tb2.aid
INNER JOIN table3 tb3 ON tb2.aid = tb3.aid
Use a case expression
BUT, you also need to look hard at how you are joining the tables. There are 2 things to note:
stop using comma separated lists of tables, there is a more precise syntax available for joins
you currently (in the question) don't have a proper join to table3
What is the best way to join 2 tables where the second table has an id and a keyword to join?
my try :
SELECT id, name
FROM table1
LEFT JOIN table2 ON (table1.id = table2.id AND table2.id = 'myKeyword')
WHERE ...
Is there a way to handle the search / join of the keyword in the WHERE clause?
Placing table2.id = 'myKeyword' in the where clause will negate the LEFT JOIN
This is quite appropriate as you have it.
Maybe with a little more detail we can see what you are getting at.
SELECT {column_list}
FROM table1 t1, table2 t2
WHERE t1.id = t2.id
AND {some_column_from_either_table} = 'myKeyword';
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
I need to make a MySQL query to show data from 3 diferents tables.
This is the table 1:
TABLE1
id
reference
name
email
This is the table 2:
TABLE2:
id
phone
This is the table 3:
TABLE3:
id
phone
I need to show all data from table1, and also the phone from table2 or table3, only if the id in table2 or table3 is the same number that is in the reference field in table1.
Any advice? Thank you!
You can try something like
SELECT t1.*
COALESCE(t2.phone,t3.phone) phone
FROM Table1 t1 LEFT JOIN
Table2 t2 ON t1.reference = t2.id LEFT JOIN
Table3 t3 ON t1.reference = t3.id
Have a look at COALESCE(value,...) and maybe SQL SERVER – Introduction to JOINs – Basic of JOINs
Yes, I have an advice, modify your structure. There's no point in having different tables to hold different phone numbers.
Here's something you can do:
table1( -- you should give it a better name
id,
-- reference, -- not needed now...
name,
email
);
phone_numbers(
id,
table1_id,
phone
);
Now you can do something like:
SELECT table1.*, GROUP_CONCAT(phone)
FROM table1
LEFT JOIN phone_numbers ON table1.id = table1_id
GROUP BY table1.id, name, email -- , whatever fields you have more on table1
You asked for a phone from table2 or from table3.
Because these 2 tables have common columns, we can simplify this whole thing and think about these 2 tables as a single one, by using an UNION clause:
select table1.*, v.phone
from table1
inner join (select * from table2
union
select * from table3) v on v.id = table1.reference
EDIT: corrected table names in the union
SELECT t1.*, t2.*, t3.*
FROM table1 t1 JOIN table2 t2
ON t1.reference = t2.ID
JOIN table3 t3
ON t1.reference = t3.ID
I don't know if you can do CASE statement in select in mysql, but you can try a CASE statement as a column and join. Here is some sudo code.
SELECT t1.*, CASE t2.phone IS NOT t3.phone THEN t3.phone ELSE t2.phone END CASE as PhoneNumber
FROM Table1 t1
LEFT JOIN Table2 t2 ON t1.reference = t2.id
LEFT JOIN Table3 t3 ON t1.reference = t3.id