mySQL joining multiple tables together - mysql

I have about 10 tables, all with varying number of columns but all contain the column 'client_id' which is the key to link all the records in each table together.
I want to grab all columns in all tables.
what would be the best way to join all 10 or so tables together on the client_id?
The reason for wanting to do this is because I want to export all tables in 1 single CSV file.

Start with a table that contains all the client ids and left join the remaining tables with the USING keyword:
SELECT *
FROM table1
LEFT JOIN table2 USING (client_id)
LEFT JOIN table3 USING (client_id)
LEFT JOIN table4 USING (client_id)
...

select *
from table1 t1,
table2 t2,
table3 t3...
where t1.user_id=t2.user_id
and t1.user_id=t3.user_id...

Related

MySQL JOIN from one of the two table based on IF condition

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.

Insert column if 2 column from different table matches

I have 2 tables:
Table1- contains PhoneNumber|Name
Table2- contains PhoneNumber|Address
I want to create Table3 with PhoneNumber|Name|Address
Table1 and Table2 may have out of order or different amount of entries, therefore table1 will act as main list.
please suggest way forward. using MySQL.
Use CREATE..SELECT with a LEFT JOIN :
CREATE TABLE Table3 AS
SELECT t1.PhoneNumber,t1.name,t2.address
FROM Table1 t1
LEFT JOIN Table2 t2
ON(t1.PhoneNumber = t2.PhoneNumber)

left join which will return all records in table 1 and add columns from table 2

I am new to MySQL having previously done everything in MS Access. I am trying to join together 2 tables so that I can show all of the records from Table1 and add in certain columns from Table2.
I can join the tables together using
SELECT Table1.Field1, Table2.Field2, Table3.Field3
FROM Table1
LEFT JOIN Table2
ON Table1.Field1=Table2.Field21
However firstly I get blanks showing for Field1 when I do this even though all of the fields are populated.
I then want to add in two further fields from Table2 into my results...
Try below code:
SELECT Table1.Field1, Table2.Field2, Table2.*
FROM Table1
LEFT JOIN Table2
ON Table1.Field1=Table2.Field2

Need to join 3 tables in SQL

I am a newbie at MySQL..... I am trying to left join 3 tables one contains some_id,name,count,descr and second one has id,some_id,uni_id and the last one has uni_id,price,added,etc So when i try to join these three tables it says that there's no such field named descr
What could be the best way to join these tables without modifying structure of them?
Assuming the following schema:
table1(some_id, name, count, descr), where some_id is the primary key;
table2(id, some_id, uni_id), where some_id is a foreign key to table1;
table3(uni_id, price, added), where uni_id is a foreign key to table2.
All you need to do is a LEFT OUTER JOIN between the three tables:
SELECT *
FROM table1 t1 LEFT JOIN table2 t2 ON (t1.some_id = t2.some_id)
LEFT JOIN table3 ON (t2.uni_id = t3.uni_id)
References:
Left Outer Join
Join Syntax
It would be ideal if you could post the schema for your tables. Without seeing the query, it sounds like you've made a reference to a field that you may have aliased to the wrong table.
At the most basic level, "descr" doesn't exist as you've tried to reference it, but beyond that, its hard to say without seeing the query itself.
SELECT descr
FROM table1
LEFT JOIN table2 ON table2.some_id = table1.some_id
LEFT JOIN table3 ON table3.uni_id = table2.uni_id
Should do the trick.

How to get data 3 tables?

can u please show me how to query 3 tables using *? thanks
You need to do a join on the tables in order to get the columns of all of them.
Warning: using * to get all columns is bad practice. You should qualify (name) all the columns you need.
Here is an example:
SELECT *
FROM table1 t1
INNER JOIN table2 t2
ON t1.key2 = t2.key2
INNER JOIN table3 t3
ON t1.key3 = t3.key3
One way you probably don't like:
SELECT *
FROM table1, table2, table3
You have to give way more information.
This generates the Cartesian product of all the three tables.