I have the following tables:
TABLE A
id
info
TABLE B
f_id
question
choices
TABLE C
f_id
question
lines
The id from the Table A always match a f_id from either Table B or C, but never both. I want to join Table B and Table C on table A only when it matches so I would get a table with the following columns :
id | info | question | choices | lines
where all rows are filled in the question column, some are NULL in the column choices and some are NULL in the column lines.
What I tried is to do two consecutive left joins, but the second one overrides the first so all the rows that doesn't match in Table C (second left join) get a NULL value in the question column.
Is there a way to do a query that will not override previously joined data with NULL values? I'm working with Laravel Eloquent, so any of raw SQL or Eloquent Query would help me.
UNION B and C and then INNER JOIN A to those results.
SELECT s1.f_id, s1.question, s1.choices, s1.lines
FROM
(
SELECT f_id, question, choices, lines = null
FROM B
UNION
SELECT f_id, question, choices = null, lines
FROM C
) s1
INNER JOIN A ON s1.f_id = A.id
but never both
Good luck with that.
id | info | question | choices | lines
SELECT a.id, a.info, b.question, b.choices, '' AS lines
FROM tableA as A
LEFT JOIN tableB AS b
ON a.id=b.f_id
UNION
SELECT a.id, a.info, c.question, '', c.lines
FROM tableA as A
INNER JOIN tableC AS c
ON a.id=c.f_id
You could use UNION to combine two different queries.
SELECT
`id`, `info`, `question`, `choices` AS `lines`
FROM
`TABLE_A` INNER JOIN
`TABLE_B` ON `TABLE_A`.`id` = `TABLE_B`.`f_id`
UNION
SELECT
`id`, `info`, `question`, `lines`
FROM
`TABLE_A` INNER JOIN
`TABLE_C` ON `TABLE_A`.`id` = `TABLE_C`.`f_id`
Make sure to use JOIN or INNER JOIN (JOIN defaults to INNER JOIN on MySQL) AND not LEFT JOIN, RIGHT JOIN, OUTER JOIN otherwise you'll end up with partially filled results.
Related
I have a couple of tables that look like this.
table_a | table_b
-------------------------
prim_key | prim_key
zero_or_one | value1
valueA | value2
valueB | value3
valueZ |
What I'm hoping to do is retrieve all of the values (prim_key, value1, value2, value3) from TABLE B if the primary keys of each table match and the value of zero_or_one in TABLE A is 0.
I'm completely new to joins, and I'm not exactly sure which join I should be using for this, but it seems like a FULL OUTER JOIN is most appropriate.
SELECT table_b.*
FROM table_a
FULL OUTER JOIN table_b
ON table_a.prim_key = table_b.prim_key
Is this even possible?
Am I using the right join for the job?
Is my "select all" syntax correct?
Since you want entries from table_b only when there is a matching primary key found in the table_a; a simple Inner Join would suffice in this case
SELECT table_b.*
FROM table_b
INNER JOIN table_a
ON table_a.prim_key = table_b.prim_key AND
table_a.zero_or_one = 0
This answer is not meant als a real answer this is meant how to simulate FULL OUTER JOIN in MySQL.
FULL OUTER JOIN is not supported in MySQL you can simulate it with a LEFT JOIN, UNION ALL and RIGHT JOIN
SELECT * FROM table_a LEFT JOIN table_b ON table_a.prim_key = table_b.prim_key
UNION ALL
SELECT * FROM table_a RIGHT JOIN table_b ON table_a.prim_key = table_b.prim_key
WHERE table_a.prim_key IS NULL
I have a table with a bunch of columns, but we only need to look at two of them. I'm trying to join another table on this table, but all we know about these two columns is that one will be null and the other won't:
client_id | note_id
The main table wants to join client_id (if not null) on clients.id OR note_id on notes.id if clients.id is null.
This will work for you. This is very basic query I wrote. Make changes if required.
SELECT * FROM YOUR_TABLE t
LEFT OUTER JOIN clients c ON t.client_id = c.id
LEFT OUTER JOIN notes n ON t.note_id = n.id
WHERE c.id IS NOT NULL OR n.id IS NOT NULL
Assuming there are 3 tables involved (the main table that contains client_id and note_id columns, clients table, and notes table), you can use a query such as this:
(select *
from mainTable inner join clients on mainTable.client_id = clients.id)
union
(select *
from mainTable inner join notes on mainTable.note_id = notes.id
where mainTable.client_id is NULL);
The above query contains 2 queries where each query will output rows where the joining column is not null. The results are then combined using union.
You can use coalesce in the join on clause. See demo here:
http://sqlfiddle.com/#!9/99911/2. If client id is null then use note id to join table1 and table2.
Select t1.client_id, t1.note_id,t2.client_id, t2.note_id
From table1 t1
Join table2 t2
on coalesce(t1.client_id, t1.note_id) =coalesce(t2.client_id, t2.note_id)
I have a table A -
SNo ID Place
1 1000 Null
2 Null Null
3 1020 CityX
And another table B -
ID Place
1000 CityY
2000 CityZ
4040 CityAA
Now, I need to join table A and B such that I can get the values of Place in table A from table B. So my final table should look like this -
SNo ID Place
1 1000 CityY
2 1020 CityX
I'm trying to create an SQL query with joins, but that is only giving me empty rows. I did -
Select * from A
left outer join B
on A.ID = B.ID
where A.ID IS NOT NULL
Where is my query breaking? How do I get the expected result?
Select A.Sno, A.ID, IF(A.Place is null, B.Place, A.place) as Place from A
left join B
on A.ID = B.ID
where A.ID IS NOT NULL
Seems you need inner join
Select A.ID, ifnull(A.Place, B.Place) from A
Inner join B
on A.ID = B.ID
where A.ID IS NOT NULL
Technically, there is nothing wrong with your query, here is a working sqlfiddle of your question
http://sqlfiddle.com/#!9/29a002/1/0
Since you didn't post your actual schema or screenshot of it, I am going to suspect incompatible column definitions or invalid data
You can just do it like this Select the Place field of table B instead.
select SNo, B.ID, B.Place
from A
left outer join B
on A.ID = B.ID
Suppose I have table A, B
ID in A is unique but in table B, ID is not unique
I want to SELECT DISTINCT ID
query 1:
SELECT DISTINCT ID FROM A a LEFT JOIN B b ON a.ID = B.ID WHERE ...
query 2:
SELECT DISTINCT ID FROM A WHERE ID IN (SELECT DISTINCT ID FROM B where ...)
or
SELECT DISTINCT ID FROM A a LEFT JOIN (SELECT DISTINCT ID FROM B) b ON a.ID = B.ID WHERE ...
The end result is same but
what happens in query 1 is the space of temp table is more as multiple rows from table B will come with repeated ID
In query 2 i am able to optimize space and further processing as it will have limited rows with all distinct ID's
Isn't there any way to use DISTINCT rows from table B using join and avoiding subqueries?
Actually I have even table C which I will join with this, so I need to care for the number of rows taking part in 2nd join when taking join further with table C.
SELECT DISTINCT ID FROM A a LEFT JOIN (SELECT DISTINCT ID FROM B) b ON a.ID = B.ID WHERE ...
Is this what you want?
Edit so the answer is a bit more visible:
Since your A is unique, but B isn't you can just swap the values :
SELECT DISTINCT ID FROM B b LEFT JOIN A a on a.ID = b.ID WHERE...
So I'm trying to do a join on multiple with the same id and each table may or may not have an entry with that id.
SELECT a.value, b.value, c.value, d.value FROM tbl_a a
JOIN tbl_b b ON a.id=b.id
JOIN tbl_c c ON a.id=c.id
JOIN tbl_d d on a.id=d.id
WHERE a.id=123
Obviously this is failing because if tbl_a doesn't have doesn't have an entry, it returns an empty resultset and the joins fail.
I've tried all sorts of left joins, outer joins and couldn't get it to work. I've also tried setting the were clause to be like: WHERE a.id=123 OR b.id=123 OR ... but that didn't work either.
I tried an ugly UNION but that gives the output in a separate row.
SELECT count(*), "a", IFNULL(a.value,0) FROM tbl_a a WHERE a.id=123
UNION
SELECT count(*), "b", IFNULL(b.value,0) FROM tbl_b b WHERE b.id=123
UNION
etc...
Any ideas?
Add the other three id fields to the WHERE clause with OR, then do outer joins.
Another approach is to create a derived table containing your desired key and LEFT JOIN-ing to all the other tables:
SELECT a.value, b.value, c.value, d.value
FROM ( select 123 as id ) as dummy
LEFT JOIN tbl_a a ON dummy.id=a.id
LEFT JOIN tbl_b b ON dummy.id=b.id
LEFT JOIN tbl_c c ON dummy.id=c.id
LEFT JOIN tbl_d d ON dummy.id=d.id