SELECT * from main table including foreign key related table - mysql

I have 3 tables.
Table A
a_id, a_name, a_description, b_id
Table B
b_id, b_name, c_id
Table C
c_id, c_name (c_name is unique hence no duplicates)
Table "A" has a foreign key 'b_id' to Table "B". Table "B" has foreign key 'c_id' to Table C
I want all Rows of table "A"(No where clause). Each row has 'b_id' so i also need row detail of that foreign key in Table "B". And row details of 'c_id' too.
How can I implement this in an efficient single query? I was using three separate queries and merging result in php. Code looked complicated. I know there is simpler and efficient way since I have just started MySQL.
I am making API that gets all these data and sends to my app.
Edit:
I am doing "SELECT *" from Table "A"
Then I am iterating the array of rows and running "Select b_name from
Table B where b_id = a.b_id"
then "Select c_name from Table B where c_id = b.c_id"
I am merging array result in the end.
What I need in result is * columns from Table A, 'b_name' from Table B and 'c_name' from Table C.

Here you need to use join
Select A.*,B.b_name as b_name,C.c_name as c_name
from A left join B on A.b_id = B.b_id left join C on B.c_id = C.c_id

Use JOIN, for example:
SELECT A.a_id, B.b_id, C.c_id FROM A JOIN B ON A.b_id = B.b_id JOIN C ON B.c_id = C.c_id
More info: http://www.w3schools.com/sql/sql_join_left.asp

Related

get values from multiple tables based on one foreign key

I have 4 tables, a,b,c,d where b,c, and d have a foreign key pointing to a's id. I want to create a query that grabs all data associated with this key from a,b,c and d. The catch is if b has an entry with this foreign key id, then c won't and vice versa. I have not been able to figure out a way to perform this query in one go. Is it possible in sql?
Try this:
select a.*, b.*, c.*, d.*
from a left join b on a.id = b.a_id
left join c on a.id = c.a_id
left join d on a.id = d.a_id

LEFT JOIN with id columns for all joined tables

I have two tables that I am applying a join to. Table A has a foreign key that references rows from Table B. SQL is as follows:
SELECT *
FROM TableA AS a
LEFT JOIN TableB AS b ON a.id = b.tableAId
WHERE a.ownerId = X
I am getting the desired result except for one thing. That is when returning the rows in JSON, only one id column is shown (TableB).
Instead I want to be able to return all id columns in the JSON where duplicate columns would have a number appended to it. For example: id, id1, id2, id3 etc...
You need to specify the columns that you want, explicitly giving them aliases so the names are different. Something like this:
SELECT a.*, b.id as b_id
FROM TableA a LEFT JOIN
TableB b
ON a.id = b.tableAId
WHERE a.ownerId = X;

delete records based on join result

I have two tables that are joined by an id.
Table A is where I define the records.
Table B is where I use the definition and add some data.
I am doing some data normalization and I have realized that on table B there are some ID that are no longer defined in table A.
If I run this query:
SELECT B.id_cred, A.id_cre from B LEFT JOIN A ON B.id_cred=A.id_cre
I see those records that are NULL on A.id_cre.
I'd like to DELETE from table B those records where the query returns null on table A?
Something like:
DELETE FROM B WHERE id IN (SELECT B.id from B LEFT JOIN A ON B.id_cred=A.id_cre WHERE a.id IS NULL)
but this query throws an error because table B is target and reference at the same time.
You can't specify target table B for UPDATE in FROM clause
Note that the join query will return 1408 rows so I need to do it in a massive way
Option 1, use NOT EXISTS:
delete from B
where not exists (select 1 from A where A.id_cre = B.id_cred)
Option 2, use a DELETE with JOIN:
delete B
from B
left join A on B.id_cred = A.id_cre
where A.id_cre is null
This should do the trick:
DELETE FROM B
WHERE NOT EXISTS (
SELECT id_cre
FROM A
WHERE B.id_cred=A.id_cre)
Just delete any row from B where the key does not exist in A.
NOTE: "A" and "B" can't be aliases, they must be the actual table names.
Hope this helps!
Why not use id_cre from A table since both have the id.
This statement:
DELETE FROM B WHERE id IN (SELECT B.id from B LEFT JOIN A ON B.id_cred=A.id_cre WHERE a.id IS NULL)
simply says that both a and b (B.id_cred=A.id_cre) are the same.

Oracle Select Query based on Column value

I have two tables lets say
Table A
columns id , name address
Table B
columns id , age, import_date
The Table B id is a reference key of Table A.
Now I want to return results from A & B but if the record is not in B I still want to see the record so for this I use left outer join
Select * from A a left join B b
on a.id = b.id
Now even I don't have record in B I still get the record.
Table B may contain duplicate ids but unique import_date.
Now I want to results in a way that if there is duplicate id in table B then I want to get the records only where import_date is as of today.
I still want to get the records for ids which are not there but if the ID is there in table B then I want to apply above condition.
I hope someone can help me with this.
Sample data
Table A
01|John|London
02|Matt|Glasgow
03|Rodger|Paris
Table B
02|22|31-AUG-2015
02|21|30-AUG-2015
02|23|29-AUG-2015
The query will return
01|John|London|null|null|null
02|Matt|Glasgow|22|31-Aug-2015
03|Rodger|Paris|null|null
You almost got the solution. Just add one more condition like below
Select a.id,a.name,a.address,b.age,b.import_date
from tablea a left join tableb b
on a.id=b.id and b.import_date=trunc(sysdate)
order by a.id;---This line optional
Check the DEMO HERE
SELECT *
FROM Table_A t1 LEFT OUTER JOIN Table_B t2 ON t1.id=t2.id UNION
SELECT *
FROM Table_A t1 LEFT OUTER JOIN Table_B t2 ON t1.id=t2.id
GROUP BY t2.import_date
HAVING t2.import_date=CURDATE();

Easy way to find out if there is no foreign key link from table A to table B?

Lets say I have table A, with an id column, and table B with an A_id column. A_id is a foreign key of id. Now, if I want to get all id's from A of which B has a foreign key link, I can do
SELECT id FROM A JOIN B ON id = A_id
However, how can I select all id's from A where B does not link to? (without selecting all id's and subtracting the above subset from that)
SELECT id
FROM a
WHERE id NOT IN
(
SELECT a_id
FROM b
)
This will use an anti-join: for each record from a, it will search b for the record's id (using an index on b.a_id) and if none found, return the record.
SELECT A.id FROM
A LEFT JOIN B ON A.id = B.A_id
WHERE B.A_id IS NULL;