I've two tables, table1 contains 22780 rows. Now I left join table1 with table2 (which doesn't contain any duplicates) and I get 23588 rows.
SELECT * FROM Table1
left join Tabelle6 ON CAST(Table1.Customer AS Int) = table2.Customer
Why do I get more rows now? I only need every row from table1 once.
Edit: found my issue, table 2 does contain duplicates. But is there any way to join every row only once and ignore any further matches?
As the comment suggests, the easiest way to handle this would probably be to do SELECT DISTINCT to remove duplicates from your result set:
SELECT DISTINCT
t1.col1,
t1.col2,
t1.Customer,
...
FROM Table1 t1
LEFT JOIN Table2 t2
ON CAST(t1.Customer AS Int) = t2.Customer
But there is another option here. We could also join to a subquery which removes duplicate customers. This would ensure that no record from the first table gets duplicated from matching to more than one record in the second table.
SELECT *
FROM Table1 t1
LEFT JOIN
(
SELECT DISTINCT Customer
FROM Table2
) t2
ON CAST(t1.Customer AS Int) = t2.Customer
Related
I'm trying to perform a SQL query like this:
SELECT
t1.*
FROM
`table1` t1,
`table2` t2
WHERE
t1.x = 1
and the table called table2 (t2) is empty but inside t1 there are entries.
For example this query works perfect:
SELECT
t1.*
FROM
`table1` t1
WHERE
t1.x = 1
So just by adding the second table t2 in the FROM part the query gives no results anymore. So I don't understand what is going on here. In my case it should be possible that one of the tables is empty but the query still needs to give results. How can I achieve this?
Your syntax is using an implied JOIN between table1 and table2, and could be rewritten as:
SELECT t1.*
FROM `table1` t1,
CROSS JOIN `table2` t2
WHERE t1.x = 1
This is JOINing everything in table1 against everything in table2. However, as table2 is empty there is nothing to join to.
With an implied CROSS JOIN, results are only returned when the row on both sides of the join is matched, which in this case it cannot be. Therefore, nothing is returned.
If you rewrote the statement to use a LEFT JOIN, you would see all results from table1, and only matching results from table2:
SELECT t1.*
FROM `table1` t1,
LEFT JOIN `table2` t2 ON 1 = 1
WHERE t1.x = 1
Incidentally, typically you would only use this kind of query if there is a relationship in the data between table1 and table2. In this case, you would JOIN on the related columns, like so:
SELECT t1.*
FROM `table1` t1,
LEFT JOIN `table2` t2 ON t2.matchedColumn = t1.matchedColumn
WHERE t1.x = 1
I have table_1 with unique records of IDs and multiple columns of data
and
table_2 with multiple rows concerning particular ID and multiple columns. One of the column in table2 is, say, time_lapse.
I need those two tables joined with all columns saved but with only those rows from table2 with highest time_lapse value.
I was trying this way...
create table as new_table
select table1.*, table2.* from
table1 left join table2
on table1.id=table2.id
where time_lapse=
(select max(time_lapse) from table2
group by id);
... but it failed.
Any suggestions for a newbie? Thank you.
You are close. But you are selecting the maximum time_lapse per id and then you act as if you had only selected one record with only one time_lapse. Use IN and have the id in the select list of your subquery:
create table as new_table
select table1.*, table2.* from
table1 left join table2
on table1.id=table2.id
where (table2.id, table2.time_lapse) in
(select id, max(time_lapse) from table2
group by id);
Then you are outer-joining table2, but want certain criteria on it in the WHERE clause. That doesn't work (as columns in outer-joined records are null).
The same query a tad prettier with a real outer join:
create table as new_table
select t1.*, t2.*
from table1 t1
left join table2 t2 on t1.id = t2.id
and (t2.id, t2.time_lapse) in
(select id, max(time_lapse) from table2 group by id);
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
Query 1:
SELECT sum(total_revenue_usd)
FROM table1 c
WHERE c.irt1_search_campaign_id IN (
SELECT assign_id
FROM table2 ga
LEFT JOIN table3 d
ON d.campaign_id = ga.assign_id
)
Query 2:
SELECT sum(total_revenue_usd)
FROM table1 c
LEFT JOIN table2 ga
ON c.irt1_search_campaign_id = ga.assign_id
LEFT JOIN table3 d
ON d.campaign_id = ga.assign_id
Query 1 gives me the correct result where as I need it in the second style without using 'in'. However Query 2 doesn't give the same result.
How can I change the first query without using 'in' ?
The reason being is that the small query is part of a much larger query, there are other conditions that won't work with 'in'
You could try something along the lines of
SELECT sum(total_revenue_usd)
FROM table1 c
JOIN
(
SELECT DISTINCT ga.assign_id
FROM table2 ga
JOIN table3 d
ON d.campaign_id = ga.assign_id
) x
ON c.irt1_search_campaign_id = x.assign_id
The queries do very different things:
The first query sums the total_revenue_usd from table1 where irt1_search_campaign_id exists in table2 as assign_id. (The outer join to table3 is absolutely unnecessary, by the way, because it doesn't change wether a table2.assign_id exists or not.) As you look for existence in table2, you can of course replace IN with EXISTS.
The second query gets you combinations of table1, table2 and table3. So, in case there are two records in table2 for an entry in table1 and three records in table3 for each of the two table2 records, you will get six records for the one table1 record. Thus you sum its total_revenue_usd sixfold. This is not what you want. Don't join table1 with the other tables.
EDIT: Here is the query using an exists clause. As mentioned, outer joining table3 doesn't alter the results.
Select sum(total_revenue_usd)
from table1 c
where exists
(
select *
from table2 ga
-- left join table3 d on d.campaign_id = ga.assign_id
where ga.assign_id = c.irt1_search_campaign_id
);
I have three tables, all of which share a common field (our_id).
our_id is the primary key in Table1.
our_id also exists zero or more times in Table2 and Table3.
I want to select records where our_id exists in both Table1 and Table2, but I also need to count how many times our_id occurs in Table3 (or even just whether our_id exists or not in Table3).
I came up with this:
SELECT Table1.*, Table2.*, COUNT(Table3.*) AS table3_count
FROM Table1, Table2, Table3
WHERE Table1.our_id = Table2.our_id
AND Table1.our_id = Table3.our_id;
…but that only returns results where our_id exists in Table3, which it may not do.
So I tried:
SELECT Table1.*, Table2.*, COUNT(Table3.*) AS table3_count
FROM Table1, Table2, Table3
WHERE Table1.our_id = Table2.our_id
AND (Table1.our_id = Table3.our_id
OR NOT EXISTS (SELECT * FROM Table3
WHERE Table1.our_id=Table3.our_id));
But that just ran and ran…
I need it to still return results where our_id exists in both Table1 and Table2, and tell me how many times (or whether) it exists in Table3.
Thank you in advance!
You should use a LEFT JOIN to bring in Table3. A LEFT JOIN returns all rows from the left table (Table1) and attempts to match the rows in the right table (Table3). If Table3 does not have any rows that match, NULL is returned for the selected Table3 fields.
SELECT Table1.*, Table2.*, t3.count
FROM Table1, Table2
LEFT JOIN (SELECT our_id, COUNT(*) AS count FROM Table3 GROUP BY our_id) t3
ON Table1.our_id = t3.our_id
WHERE Table1.our_id = Table2.our_id