Select non-duplicate records in a MySQL table column - mysql

I have a large table with just 2 column. One is the primary id column and other is a data column.
I need to select only the records that is not-duplicated in the table. I tried the below query but it takes much time and not sure if it really work.
select * from (select column_name
from table_name
group by column_name
having count(*) = 1) x;
What do you think?
I am also open to other tries if it will do the job faster.

You can left join the same table or use subquery to check for duplicates.
This should be easier for SQL server as it would not count all duplicates.
Something like this:
SELECT
t1.column_name
FROM
table_name AS t1
WHERE
NOT EXISTS (
SELECT
*
FROM
table_name AS t2
WHERE
t2.column_name = t1.column_name
AND t2.id != t1.id
)
OR
SELECT
t1.column_name
FROM
table_name AS t1
LEFT JOIN table_name t2 ON (
t2.column_name = t1.column_name
t2.id != t1.id
)
WHERE
t2.column.name IS NULL

Related

Update query with multiple tables and limit option

Update table2 t2
set t2.c1=t1.c1
inner join table1 t1 on ( t1.c2 = t2.c2)
where t2.c1 = "-1";
I want to execute the above query which will update the table2 column from table1 column ON INNER JOIN matching conditions. It is working fine. I am running a migration where the rows count are in million in both tables. I thought of limiting the update query in batches for query optimization but limit is not allowed in update query.
I can try with select query with limit option, but updating multiple columns would not work with this below query.
update table2 t2
set t2.c1=<?>
where t1.c2 = ( select c2 from table);
Can anyone help to use update query with optimization? Will updating millions row have any impact?
You could move the limiting clause to the joined table, like so:
update table2 t2
inner join (
select c1, c2
from table1
order by c2
limit ?, ?
) t1 on t1.c2 = t2.c2
set t2.c1 = t1.c1
where t2.c1 = -1
I am not sure what you really want to do, but you can update multiple columns with update and limit.
The following is fine:
update table2 t2
set t2.c1 = <?>,
t2.c3 = ?
where t1.c2 = ( select c2 from table)
limit 100;

Get id of the record having Min() value

I have a complex mysql query where one of the Select fields is Min(value). Since all the 'values' are unique, is there also a way to get found min value's row id along?
In other words if we simplify the query to this question, it is like this:
SELECT t1.name, MIN(t2.value) AS minval
FROM table1 t1
LEFT JOIN table2 t2
ON t2.id_user = t1.id
GROUP BY id_user
How can i now know which t2.id was chosen for lowest t2.value for particular user? Thank you!
Use ROW_NUMBER() to find the first value of each id_user
You can replace * with the fields you need
SELECT *
FROM (
SELECT *, ROW_NUMBER() OVER (PARTITION BY t2.id_user ORDER BY t2.value) as rnk
FROM table1 t1
LEFT JOIN table2 t2
ON t2.id_user = t1.id
) as X
WHERE X.rnk = 1
Maybe this simple, dont know how complex your statement is:
SELECT name,value,id
FROM(
SELECT t1.name,t2.value,t2.id
FROM table1 t1
LEFT JOIN table2 t2
ON t2.id_user = t1.id
GROUP BY t2.id,id_user
ORDER BY t1.name,t2.id asc) as test
GROUP BY name;

Mysql multiple table joins with derived tables

My Table Details image with test data
I have come up with below code:
select tableName,userDets,regDate
from (
(SELECT 'Table1' AS tableName, t1.username as userDets, t1.created_date as regDate FROM table1 t1 group by username )
union all
(SELECT 'Table2' AS tableName, t2.username as userDets, t2.created_date as regDate FROM table2 t2 group by username )
union all
(SELECT 'Table3' AS tableName, t3.username as userDets, t3.created_date as regDate FROM table3 t3 group by username )
) AS allTableCombo
group by allTableCombo.userDets
If in this code i use order by date at last and then make it subquery for another select with group by username , I can obtain required output. But I dont want to use sbuquery.
Please if anyone could provide me with an alternative.. I tried with self join but was not success on using it....

Whats wrong with MySQL query?

I am trying to delete rows from a table that spit out from a join:
DELETE FROM t1 WHERE company_name IN
(SELECT company_name FROM t1
LEFT OUTER JOIN t2
ON t2.company_name = t1.company_name
WHERE t2.name IS null)
Column 'company_name' in field list is ambiguous
Getting this ambiguous error while trying to make this query? Any suggestions?
MySQL doesn't like it when you try to UPDATE/DELETE a table and SELECT from the same table in the same query.
You can solve this with multi-table DELETE syntax:
DELETE t1 FROM t1 LEFT OUTER JOIN t2 USING (company_name)
WHERE t2.name IS NULL;
As the error message tells the column name company_name is not unique. Depending on your needs I believe this may solve your problem assuming you're trying to delete entries in t1 which doesn't have a corresponding row in t2 or have one but with name is null:
DELETE FROM
t1
WHERE
company_name NOT IN (
SELECT
t2.company_name
FROM
t2
WHERE
t2.name IS NOT NULL
)
Try that
DELETE FROM t1 WHERE company_name IN ( select * from (SELECT t1.company_name FROM t1 LEFT OUTER JOIN t2 ON t2.company_name = t1.company_name WHERE t2.name IS null) t )

inner join for a query?

I want to do a sql query and have some problems:
I want to select from table_1 the ID's Where parent_id is the value I have:
SELECT ID
FROM table_1
WHERE parent_ID = 'x'
I want to use the ID'S I got in 1. and
SELECT
FROM table_2
WHERE ID = 'The ID's from Query 1.'
Like this?
select ...
from table_1 a
join table_2 b on(a.id = b.id)
where a.parent_id = 'x';
Edit
Note: the query will potentially produce duplicate rows depending on the keys and relation between the tables. For example, you will get duplicates if, for a given table_1.parent_id = X, there can be multiple occurrences of the same table_1.ID.
Another example is when table_2.ID isn't unique.
In those cases you would want to remove the duplicates (using distinct, group by, partitioned #row_number, etc) or, not produce the duplicates in the first place using a semi-join instead (exists, in). Have a look #OMG Ponies answer for reference.
Using IN
SELECT t2.*
FROM TABLE_2 t2
WHERE t2.id IN (SELECT t1.id
FROM TABLE_1 t1
WHERE t1.parent_id = 'x')
Using EXISTS
SELECT t2.*
FROM TABLE_2 t2
WHERE EXISTS (SELECT NULL
FROM TABLE_1 t1
WHERE t1.id = t2.id
AND t1.parent_id = 'x')
Using an INNER JOIN
The DISTINCT (or GROUP BY) is necessary to eliminate duplicates if there are more than one records in TABLE_1 that relate to a record in TABLE_2:
SELECT DISTINCT t2.*
FROM TABLE_2 t2
JOIN TABLE_1 t1 ON t1.id = t2.id
AND t1.parent_id = 'x'
It can be solved with the use of IN as follows:
SELECT * FROM table_2 WHERE ID IN (SELECT ID FROM table_1 WHERE parent_ID = 'x')
select * from table_2 where id in (select id from table_1 where parent_id = 'x')
Yes, it's better to you use this:
SELECT [value]
FROM [table2]
WHERE [value] IN (SELECT [value]
FROM [table1]
WHERE [value] = "[value]"
)