I'm desperate with this query. I have two tables table1 and table2, tables are identical but they have different data. I'm trying to remove duplicities by columns code and manufacturer. To do that I need in final result ID from table1 ID from table2 and also columns code and manufacturer
SELECT * FROM (
SELECT id,code,manufacturer FROM table1 WHERE manufacturer = 1
UNION SELECT id,code,manufacturer FROM table2 WHERE manufacturer = 1
) AS t GROUP BY code HAVING COUNT(*) > 1
But in result i got only values from table1. It's OK but I just need to get there id from table2 too. Please can anyone give me some tips how to do this ?
You have two basic problems:
Problem 1:
You are using UNION when you should be using UNION ALL, because UNION removes duplicates!
Problem 2:
This isn't the right way to go about the problem. You should be using a simple join, not a union.
Try this:
SELECT
t1.id as table1_id,
t2.id as table2_id,
t1.code,
t1.manufacturer
FROM table1 t1
JOIN table2 t2
ON t2.code = t1.code
AND t2.manufacturer = t1.manufacturer
WHERE manufacturer = 1 -- this WHERE clause is optional
Your use of the WHERE clause is a little odd - consider removing it to get all duplicates from all manufacturers.
Related
I have two tables.
table1 has a row id='12345'
table2 has a row where the id = that of table1 with added chars, e.g.,
table2.id = '12345-678qt'
table2 may have more than one id starting with '12345-' with different ending chars.
Yes, there is always a dash after table1's id that could be used in the query.
I need to get some data from both tables, say
SELECT table1.id, table1.field9,
table2.id, table2.fieldZ
FROM table1 and table2
WHERE (
table1.id=table2.id's characters before the dash
OR
table1.field1 = 'abcde'
)
AND table2.dataB='something'
ORDER BY table1.datefield DESC LIMIT 3;
Thank you.
The MySQL translation of:
"table1 and table2 WHERE" is table1 INNER JOIN table 2 ON
"table2.id's characters before the dash" is SUBSTRING_INDEX(table2.id, '-', 1)
The query should look like this:
SELECT table1.id, table1.field9,
table2.id, table2.fieldZ
FROM table1
INNER JOIN table2
ON (table1.id = SUBSTRING(table2.id, '-', 1) OR table1.field1 = 'abcde')
AND table2.dataB = 'something'
ORDER BY table1.datefield DESC
LIMIT 3;
This doesn't grant that your query will work if it's written like this. My fixes will remove the current errors inside your query. For more troubleshooting with a sql fiddle link, please provide tables and I'll be happy to help.
Check the official documentation about the JOIN operation and the SUBSTRING_INDEX function at the corresponding links.
For question purposes I will use minimal data for my examples.
I have a table called table1 and a column named test that looks like this:
test
5012
I am able to add an extra zero behind before the result of column test using this query:
SELECT CONCAT('0',test) as x from table1
this is the result of the query:
results table: table1
x
05012
Now I have another table called table2 looking like this:
test test2
05012 1
My question is, how do I join the two tables together based on that query above and concat the table1 with column test2 from table2? Making sure the first 4 characters of both columns test from both tables match together?
This is how table 1 should look like:
Afterquery
050121
I am curious why you wouldn't simply use table2?
select concat(t2.test, t2.test2) as afterquery
from table2 t2;
table1 doesn't seem to play a role.
If you want values in table2 filtered by table1, you can use exists:
select concat(t2.test, t2.test2) as afterquery
from table2 t2
where exists (select 1
from table1 t1
where t2.test = concat('0', t1.test)
);
You can express this as a join:
select concat(t2.test, t2.test2) as afterquery
from table2 t2 join
table1 t1
on t2.test = concat('0', t1.test);
This is useful if you want columns from both tables -- but that is not necessary to answer the question. On the other hand, this runs the risk of duplication if there are multiple matches.
I think that this should be the solution. You need to use concat in the join between table1 and table2
SELECT CONCAT('0', table1.test, table2.test2) AS Afterquery
FROM table1
INNER JOIN table2
ON CONCAT('0',table1.test) = table2.test
Slightly different approach with a sub-query:
select concat(concat_test, test2) test_results
from
(select concat('0', test) concat_test from table1) table_alias
join
table2 on substring(concat_test,1,4) = substring(test,1,4);
I have a database with 2 tables. table1 and table2.
Table1 contains a list of tests ( column 'name' ). And I want to count how many rows are missing in table2 ( this table has also a column 'name' ) according to the list of tests of table1.
So I just want to count the mismatch between table1.name and table2.name.
I tried several querys, but all didnt really work.
I tried to use the 'NOT IN' statement but it takes too much time. Like several minutes.
For example, the output should be :
COUNT(*) = 20
It means that 20 tests are missing ( or not done yet ) in table2.
I'm using MySQL, so I can't use EXCEPT or MINUS statement.
Thank you by advance.
Nordine
You can use not exists :
select count(*)
from table1 t1
where not exists (select 1 from table2 t2 where t2.name = t1.name);
If you have a duplicate name in table1 then you need count(distinct t1.name) instead.
Try the below query:
select count(case when bname is null then 1 end)
from
(
select a.name as aname, b.name as bname from
table1 a left join table2 b
on a.name=b.name)x
MINUS can be used in MySQL.
Ref:http://www.mysqltutorial.org/mysql-minus/
Try this:
SELECT name
FROM table1
MINUS
SELECT name
FROM table2
I need to show only results which are in Table1 and Table2 but are not in Table3. Basically, it should be something like TABLE1, Table2 except INNER JOIN between (TABLE1, Table2) and TABLE3.
Should looks like this - On left side Table1 and Table2, on right side Table3
Now I have this:
SELECT mesta_email, mesta_kod
FROM Table1
UNION ALL
SELECT mesta_email, mesta_kod
FROM Table2
// And somehow except values which are in Table3
Can somebody help me please? Thanks a lot.
There are a couple different ways to do this. I believe mysql does better with the outer join/null approach:
select t.*
from (
SELECT mesta_email, mesta_kod
FROM Table1
UNION ALL
SELECT mesta_email, mesta_kod
FROM Table2
) t left join Table3 t3 on t.mesta_email = t3.mesta_email
and t.mesta_kod = t3.mesta_kod
where t3.mesta_email is null
This assume table3 shares the same structure as the other 2 tables.
I would approach the problem almost directly as you write it, using exists and not exists:
select t1.mesta_email, t2.mesta_kod
from table1 t1
where exists (select 1
from table2 t2
where t2.mesta_email = t1.mesta_email and t2.mesta_kod = t1.mesta_kod
) and
not exists (select 1
from table3 t3
where t3.mesta_email = t1.mesta_email and t3.mesta_kod = t1.mesta_kod
);
One advantage of exists/not exists over other approaches involves duplicates. If one of the tables (say table1) has not duplicates, but the others might, there is no need to remove duplicates in the resulting data set.
If I have this three tables:
table1: id, title, content
connection: id_t1, id_t2
table2: id, title, content
In my case I just select a single row of table1. For this result there are many rows in table2. The connection of both tables can be found in the table 'connection'
How do I have to create the query to get this result?
table1-title
table2-content1
table2-content2
table2-content3
table1-content
If I understand correctly I believe you want to use GROUP BY with the GROUP_CONCAT function
The query would look something like this:
SELECT table1.title, GROUP_CONCAT(table2.content) as table2.group_content, table1.content
FROM table1
JOIN connection on table1.id = id_t1
JOIN table2 on connection.id_t2 = table2.id
GROUP BY table2.content
This would give you one row for each table1.id, with multiple table2.content rows concatenated into one column (called table2.group_content in this example).
select title from table1 where title_id = 1
UNION
select t2.content
from table2 t2, table1 t1, connection c
where t1.title_id = 1
and t1.title_id = c.id_t1
and c.id_t2 = t2.title_id
UNION
select content from table1 where title_id = 1