Join two tables and show where it come from - mysql

I have two tables:
ID, actor, description....
FK_ID, other_description...
I will now join these two tables, so that I have the following result:
ID|actor|description|other_description
1, Bud Spencer, >MainDescription<, null
1, Bud Spencer, null, Other_Description1
1, Bud Spencer, null, Other_Description2
.....
How can I write my query?
Thank you.

if you need the value in the same row You can use a left join on id and fk_id
select a.ID, a.actor, a.description, b.other_description
from table1 as a
left join table2 as b on a.ID = b.FK_ID
if you need the distinct value in the same column you can use union
select ID, actor, description
from table1
union
select fk_ID, null, other_description
from table2
if you need all the value you can use union all

SELECT A.ID A.actor A.description B.other_description
FROM tableA A
INNER JOIN tableB B ON
A.ID = B.FK_ID

SELECT a.ID, a.actor, a.description, b.other_description
FROM DB1 a
JOIN DB2 b
on a.ID=b.FK_ID

Related

Make 3 paired columns from one Mysql

I have a problem with a query that I try to make over a table in database.
So my table has a column with names like this :
I want a query that will select names and ids in the following mode:
either
or
Thanks in advance...
You can use inner join and calc the id
#my_len = 3
select a.id as id, a.name as name, b.id as id1, b.name as name1, c.id as id2, c.name as name2
from my_table as a
inner join my_table as b where a.id = b.id+#my_len
inner join my_table as c where a.id = c.id+#my_len*2

SQL Join over two tables

here is my problem:
I have two tables. Every entry of table A has several entries in table B matched over an ID. I now want to get all entries of table A with one data entry of table B - the one with the highest ID in this table.
Table A has an ID
Table B has an own ID and ID_OF_TABLE_A (for the relation between both)
Table A has one to many relation to Table B. I want all Entries of Table A, matched with the one with the highest ID out of B. Is there any way to realize this in an SQL Statement? I tried all kinds of joins since I need the information of that matched entry in the outcome of the select.
How about
SELECT *
FROM tableA a
INNER JOIN tableB b ON a.ID = b.ID_OF_TABLE_A
WHERE b.ID = (SELECT MAX(ID) FROM tableB c WHERE b.ID = c.ID)
Try this:
SELECT a.*, b.*
FROM tableA a
LEFT OUTER JOIN (SELECT b.*
FROM tableB b
INNER JOIN (SELECT ID_OF_TABLE_A, MAX(ID) bID
FROM tableB
GROUP BY ID_OF_TABLE_A
) c ON b.ID_OF_TABLE_A = c.ID_OF_TABLE_A AND b.ID = c.bID
) AS b ON a.ID = b.ID_OF_TABLE_A;
You can use an inline view where you filter the rows you need from table b with the help of the grouping clause and max function like so:
select a.*, b.*
from a
join (
select max(id) as id_b_max, id_a
from b
group by id_a
) b
on a.id = b.id_a;
Tested with:
create table a(id int);
create table b(id int, id_a int);
insert a values (1);
insert b values(1, 1);
insert b values(2, 1);
insert b values(3, 1);
select a.id, max(b.id)
from table_a a join table_b b on a.id = b.table_a_id
group by a.id
This should work. prefilter out the max id of the ID_OF_TABLE_A. and then join on that id.
SELECT A.*, B.*
FROM A
INNER JOIN ( SELECT max( ID ) AS id, ID_OF_TABLE_A
FROM B
GROUP BY ID_OF_TABLE_A) AS grp_b
ON grp_b.ID_OF_TABLE_A = a.ID
INNER JOIN B ON b.ID = grp_b.id

Outer Join on multiple tables

select table3.tid, table3.name, talble4.name1, table4.name2 from table3 left join
(select table1.tid, table1.name as name1, table2.name as name2 from table1 left join table2
on table1.tid = table2.tid
union
select table2.tid, table1.name, table2.name from table1 right join table2
on table1.tid = table2.tid) as table4;
Please tell me what is wrong here.
I want a full outer join of the 3 tables : table1, table2 and table3 (which MYSQL does not support)
I would emulate a "full outer join" of three tables with three separate queries, with the rows concatenated together with UNION ALL operations.
The first query is all the tid values from table1. The second query gets all the tid values from table2 which don't exist in table1. The third query gets all the tid values in table3 which don't exist in table1 and don't exist in table2. The "trick" in the second and third queries is including appropriate
tid IS NULL
predicates in the WHERE clause, to make sure the tid values returned from the prior queries are omitted. (If we aren't guaranteed that tid is NOT NULL, we would probably want to avoid returning NULL values for tid by including an appropriate tid IS NOT NULL predicate in each of the queries for the "driving" table, which in the example below is the table following the FROM keyword.)
The last step is to include the name columns in the select list. For consistency, I'd put the name value from table1 in the same column. (In the second query, the name1 column will always be NULL, in the third query, the name1 and name2 columns will always be NULL.)
SELECT a.tid
, a.name AS name1
, b.name AS name2
, c.name AS name3
FROM table1 a
LEFT
JOIN table2 b ON b.tid = a.tid
LEFT
JOIN table3 c ON c.tid = a.tid
UNION ALL
SELECT d.tid
, f.name AS name1
, d.name AS name2
, e.name AS name3
FROM table2 d
LEFT
JOIN table3 e ON e.tid = d.tid
LEFT
JOIN table1 f ON f.tid = d.tid
WHERE f.tid IS NULL
UNION ALL
SELECT g.tid
, h.name AS name1
, i.name AS name2
, g.name AS name3
FROM table3 g
LEFT
JOIN table1 h ON h.tid = g.tid
LEFT
JOIN table2 i ON i.tid = g.tid
WHERE h.tid IS NULL
AND i.tid IS NULL
Hi you don't seem to have an "on table3.JoinColumn = table4.JoinColumn" after table4 in your SQL. I think you've been marked down as you didn't say what your error is, and I think your question is a little vague. But perhaps the SQL I've given may be all your need to complete your task...

LEFT JOIN 2 Table but will only return the 1st Record from Table2

table1
cid
itemdesc
itemprice
table2
cid
imagename
status
My 1st table is has unique cid (no duplicate) I want it to LEFT JOIN TO table2 but it has multiple rows per cid
cid imagename status
1 image1-of-cid1 test1
1 image2-of-cid1 test2
2 image1-of-cid2 test3
2 image2-of-cid2 test4
2 image3-of-cid2 test5
But I only want the Query to return the the 1st row only of the each record fom table 1
Thanks
I agree with John Woo's answer above. You need a subquery of some kind to actually retrieve the first row of table 2. Something like:
SELECT
t1.[id],
t2.*
FROM table1 AS t1
LEFT JOIN table2 AS t2
ON t2.cid = (SELECT TOP 1 cid FROM table2 WHERE cid = t1.cid)
you need to create an extra subquery that gets one imagename per cid. try this,
SELECT a.*, b.*
FROM table1 a
LEFT JOIN
(
SELECT cid, MIN(imagename) minImage
FROM table2
GROUP BY cid
) c ON a.cid = c.cid
LEFT JOIN table2 b
ON c.cid = b.cid AND
b.imageName = c.minImage
SQLFiddle Demo
Select
distinct a.cid,a.itemdesc,b.imagename,a.itemprice,b.status
from table1 a,
table2 b
where a.cid=b.cid
Try this:
SELECT a.cid, a.itemdesc, a.itemprice, b.imagename, b.status
FROM table1 a
LEFT OUTER JOIN table2 AS b ON a.cid = b.cid
GROUP BY a.cid, a.itemdesc, a.itemprice;

MySQL syntax for JOIN depending on condition

What would be a syntax of the following query:
Get all columns from Table1 and JOIN Table2 if matching reference (Table1ID) exists, otherwise JOIN Table3.
Simplified DB structure is more or less as below
Table1
ID Type
1 std
Table2
ID Table1ID Title Language
1 1 Test en
Table3
ID Table1ID Title Language Flag
1 1 Other en 1
Also, I now realized that Table3 will have multiple entries that refer to single Table1.id. How to limit it to return only the latest entry (with highest id) for every result?
If you don't want an entire separate set of columns for each join, this may be what you're looking for:
SELECT *
FROM (
SELECT a.ID AS Table1ID, a.Type, b.ID, b.Title, b.Language, NULL AS Flag
FROM Table1 a
JOIN Table2 b ON a.ID = b.Table1ID
UNION ALL
SELECT a.ID, a.Type, c.ID, c.Title, c.Language, c.Flag
FROM Table1 a
LEFT JOIN Table2 b ON a.ID = b.Table1ID
JOIN Table3 c ON a.ID = c.Table1ID
JOIN (
SELECT MAX(id) AS maxid
FROM Table3
GROUP BY Table1ID
) d ON c.ID = d.maxid
WHERE b.ID IS NULL
) a
ORDER BY a.Table1ID
SQLFiddle Demo
this is one way to do it.
select table1.id, table1.type, ifnull(table2.title, table3.title)
from table1
left join table2 on table1.id = table2.table1ID
left join table3 on table1.id = table3.table1ID