sql join three table one maybe empty - mysql

I have three tables, two certainly with data values, one the values can be present or not.
This is an example schema
Table 1
id, username
Table 2
id, street
Table 3
id, phone_number (this can be not present)
please help me with query

SELECT t1.ID, t1.Username, t2.street, t3.phone_number
FROM Table1 as t1
INNER JOIN Table2 as t2 on t1.id = t2.id
LEFT OUTER JOIN Table3 as t3 on t1.id = t3.id

Related

Create view by selecting different columns from multiple tables in SQL server

Im trying to create a view where specific Columns from 2 tables to be combined.
My Tables
Table1(Column1,Column2,Column3)
Table2(Column4,Column5,Column6)
Expected output
View1(Column2,Column3,Column6)
What query can i use to achieve that output?
CREATE VIEW [dbo].[View]
AS
SELECT a.Column2,a.Column3,b.Column6
FROM Table1 AS a
INNER JOIN Table2 AS b ON A.ID = B.ID -- your logic
If you have and id to join tables:
create view View1
select t1.column2, t1.column3, t2.column6
from table1 t1
join table2 t2 on t1.id = t2.id
If you don't have and id and want to get mixes columns without relationships:
create view View1
select t1.column2, t1.column3, t2.column6
from table1 t1
cross join table2 t2 on t1.id = t2.id
As Fourat and jarhl commented above, you need to explicitly define what columns are used to JOIN both tables. So using the below code change the ID column to the column that relates these two tables, and if it's more than one column the list them all separated by AND.
CREATE VIEW dbo.YourViewName
AS
SELECT t1.Column2, t1.Column3, t2.Column6
FROM Table1 t1
JOIN Table2 t2 ON t1.ID = t2.ID
GO

I have two tables,first table T1 contains id and name and the name contains 5 values

I have two tables,first table T1 contains id and name and the name contains 5 values.And the second table T2 contains id and amonut_paid and the amount paid column contains 3 values.I want to display the all names from the table T1 and the amount_paid in sql
Do a LEFT JOIN and then use IFNULL for replacing the NULL with 0
SELECT T1.name,IFNULL(T2.amount_paid,0)
FROM T1
LEFT JOIN T2 ON T1.id=T2.id
Note: Instead of IFNULL you can also use COALESCE as strawberry suggested.
Use left join here, Because your t1 table contains 5 names and you want to display all name from t1 table. And your t2 table contains only three entry(amount_paid).
Select t1.name, t2.amount_paid
from t1
left join t2 on t1.id = t2.id
Use an LEFT JOIN on both these tables T1 and T2, so that it shows only the rows where a NAME against AMOUNT_PAID exists (INNER JOIN provides the rows available in both these tables)
SELECT T1.name, T2.amount_paid
FROM T1
LEFT JOIN T2 ON T1.id = T2.id
--RETURNS 3 rows as per your example
Use an OUTER JOIN on both these tables T1 and T2, so that it shows all the rows irrespective of the values being present in these tables.
RETURNS 5 rows as per your example

MySQL inner join retrieve table 1 even though table 2 does not matched ID

I have this query that basically retrieve 2 tables (tbl_users and tbl_moreInfo) based on selected user ID. It works fine but there are times that the tbl_users has the ID that the tbl_moreInfo doesn't have so it returns empty row. I would still want to retrieve tbl_users even though there are no ID matched in tbl_moreInfo in just 1 query.
This is my query:
SELECT T1.*, T2.*
FROM tbl_users T1
INNER JOIN tbl_moreInfo T2
ON T1.ID=T2.ID
WHERE T1.ID=1
Thanks in Advance :)
First, You should use in table 2 a reference id of table 1
JOIN tbl_moreInfo T2 ON T1.ID = T2.T1_ID -- NOT T2.ID
And then, if id doesn't exist in table 2, use left join, so filled with null in the data that do not exist.
SELECT T1.*, T2.*
FROM tbl_users T1
LEFT JOIN tbl_moreInfo T2 ON T1.ID=T2.ID
WHERE T1.ID=1

Why query show only first row?

We have 2 tables - Table1 and Table2
rows in Table1
id name
1 test
2 test2
Table2 is empty, structure:
id table1_id
query:
SELECT
t1.name as name,
count(t2.id) as count_show
FROM
Table1 as t1
LEFT JOIN
Table2 as t2 on t2.table1_id= t1.id
In result we see:
name count_show
test 0
We know that problem with count, but why? where error?
Why we get only first row and how output all rows?
Answer as requested:
You're doing a count. It is automatically grouped. So if the count of your row is only one row, that's what you get.
You have to add Group by t1.id To the end of your query.
The final query:
SELECT t1.name AS name, COUNT(t2.id) AS count_show
FROM Table1 AS t1 LEFT JOIN Table2 AS t2 ON t2.table1_id = t1.id
GROUP BY t1.id

MYSQL select from 3 or more tables

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