Distinct value from multiple tables with UNION - mysql

So I have this mysql statement and I want to get the value of id just once. This is what i've tried with but it still writes the ids multiple times.
SELECT table1.id, table3.location FROM table1, table3
UNION
SELECT table2.id, table3.location FROM table2, table3
GROUP BY id
It gives example:
1 - swe
1 - swe
2 - uk
2 - uk
etc
I want:
1 - swe
2 - uk
Any suggestions anyone? Greatful for any help! Thanks

Just leave the group by:
SELECT table1.id, table3.location FROM table1, table3
UNION
SELECT table2.id, table3.location FROM table2, table3
As pointed out by W3C, UNION already shows only distinctive values by default:
http://www.w3schools.com/sql/sql_union.asp

You no need to add group by clause cause UNION will already distinct your 2 result sets.
SELECT table1.id, table3.location FROM table1, table3
UNION
SELECT table2.id, table3.location FROM table2, table3
SQL Fiddle Demo

SELECT t1.id, t3.location FROM table1 t1
inner join table3 t3 on (t1.id = t3.id)
UNION
SELECT t2.id, t3.location FROM table2 t2
inner join table3 t3 on(t2.id = t3.id)
Group by 1;

Related

Combining two Access Queries

I three tables. Table1, Table2, Table3. They all have a field called KB. Can I have one query to accomplish: Get all the information from table2, when Table1 KB = Table2 KB But only if table1 KB doesn't exist in Table3. I currently have 2 Queries. One to determine which KBs in Table1 don't exist in Table3. I then run a query against those results showing me all the records in Table2 that have matching KB. Can I do that all in 1 query, or 2 queries the best way?
You can do it with a combination of EXISTS and NOT EXISTS:
SELECT t2.*
FROM table2 AS t2
WHERE EXISTS (SELECT 1 FROM table1 AS t1 WHERE t1.KB = t2.KB)
AND NOT EXISTS (SELECT 1 FROM table3 AS t3 WHERE t3.KB = t2.KB)
or with an INNER join of table2 to table1 and a LEFT join of table2 to table1 from which you will return only the non matching rows:
SELECT t2.*
FROM (table2 AS t2 INNER JOIN Table1 ON t2.KB = Table1.KB)
LEFT JOIN table3 ON t2.KB = table3.KB
WHERE table3.KB IS NULL
This may return duplicate rows of table2 if the relationship of table2 and table1 is 1:n, so in this case you can use DISTINCT:
SELECT DISTINCT t2.*
....................

Join two tables based on a view, sql

I have the following sql situation
Table1
id name
Table2
id name
_Table1ToTable2
id, id_table1, id_table2
Result:
id, name, table2_name, table2_id
I would like to join Table1 and Table2 into a single query, but I cannot find a way to make a join between them, any ideas?
It seems like you want to use bridge table Table1ToTable2 to join Table1 and Table2. This can be done simply with 2 INNER JOINs :
SELECT
tt.id AS tt_id,
t1.id AS t1_id,
t1.name AS t1_name,
t2.id AS t2_id,
t2.name AS t2_name
FROM
Table1ToTable2 AS tt
INNER JOIN Table1 AS t1
ON t1.id = tt.id_table1
INNER JOIN Table2 AS t2
ON t2.id = tt.id_table2
if there is no relationship between table1 and table 2 then,
select table1.id, table1.name, table2.id, table2.name from
table1, table2
if table1 and table2 are related with ID then,
select table1.id, table1.name, table2.id, table2.name from
table1
inner join table2
on table1.id = table2.id
If your intention is to retrieve a result set with every possible combination of the id columns from both tables then you can do as follows:
select id = identity (int, 1, 1), * into #a from (select distinct table1. id as id1, table2. id as id2 from table1 cross apply table2) d; select * from #a; drop table #a
If you do not wish to use a temp table an alternative would be to use some variation of the row_number() function.
The above solves for an assumed requirement as your question lacks context. Elaborate on your requirements and we will be able to provide you with a more relevant and detailed answer.

How to combine 2 columns from 2 tables and subtract duplicates

I have 2 tables t1 and t2. Each have a customer ID column. What I am looking for is to join the 2 columns and SUBTRACT the duplicates.
My EG:
Table1 and Table2 with the IDs for each
I have tried a union query. The result I am left with is ID = 1,2,3,4,5,6,7,8,9,10. Where, what I'm after is subtracting 1-5 from Table2 and the result = 6,7,8,9,10.
I hope that makes sense and that someone is able to help. Sorry if this is a bit too simple compared to what you're all used to.
In SQL Server you can use the EXCEPT operator:
select ID
from Table2
except
select ID
from Table1
Mysql does not support it though. Using a an in clause or a left join would work in both servers:
--Using In clause
SELECT ID
FROM Table2
WHERE ID NOT IN
(
SELECT ID
FROM Table1
);
--Using join
SELECT Table2.ID
FROM Table2
left join Table1
on Table2.ID = Table1.ID
where Table1.ID is null
Use left outer join
select * from t1 left outer join t2 on t1.customerid = t2.customerid

SQL optional parameter?

Lets say I have two tables:
Table1
Id Name
1 Joe
2 Greg
3 Susan
4 Max
Table2
Uid comment
2 Good customer
4 Great guy
What I want to do is list all elements of Table 1, and if Table1.Id = Table2.Uid I want to select this comment. If comment does not exist give blank field.
Result should be:
1 Joe
2 Greg Good customer
3 Susan
4 Max Great Guy
I can't figure out how to do it, if I write:
select
table1.Id,
table1.Name,
table2.comment
where
table1.id=table2.Uid
It gives me only users 2 and 4.
Try to use left join it shows you all data from table1
select t1.Id, t1.Name, t2.comment
from table1 t1
left join table2 t2 on t1.id=t2.Uid
NOTE:
Good practice is to use aliases as above. Code is more readable.
select
table1.Id,
table1.Name,
table2.comment
from table1 left outer join table2 on table1.id=table2.Uid
It is a classical JOIN operation:
SELECT
t1.id, t1.name, t2.comment
FROM
Table1 AS t1
LEFT JOIN
Table2 AS t2 ON t1.id = t2.uid

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