Finding all children with duplicate columns - mysql

I am trying to find duplicates in a table column only if the row is also a child of another table ex:
table 1 columns
id
Type
table 2 columns
id
table1Id
table3Id
Sample Data:
table 1:
id Type
1 aType
2 myType
3 myType
4 myType
5 myType
6 myType
table 2:
id table1Id table3Id
1 1 1
2 2 1
3 4 2
4 5 1
5 6 2
Results I'd like: (rows in table1 with same Type and table3Id)
table1Id table1Type table3Id
2 myType 1
5 myType 1
4 myType 2
6 myType 2
query I tried:
select t1.id as table1Id, t1.type as table1Type, t2.table3Id
from table1 t1 inner join
table2 t2
on t1.id = t2.table1Id inner join
table1 a
on t1.Type = a.Type and a.id <> t1.id
where t1.Type = 'myType' ;
The above query gives me hundreds of repeats of the same row, with around 500,000 rows returned.

You seem to want pairs of table3id and table1type that are the same. Here is a method that returns results in a slightly different format:
select t2.table3id, t1.type, group_concat(t1.id) as table1ids
from table1 t1 join
table2 t2
on t1.id = t2.table1id
group by t2.table3id, t1.type
having count(*) > 1;
This puts the ids with the same value in a list.

This works fine for me:
drop table if exists table1;
drop table if exists table2;
create table test_delete.table1 as
select 1 id , 'aType' typ union all
select 2 ,'myType' union all
select 3 ,'myType' union all
select 4 ,'myType' union all
select 5 ,'myType' union all
select 6 ,'myType';
create table test_delete.table2 as
select 1 id , 1 table1Id , 1 table3Id union all
select 2 ,2 , 1 union all
select 3 ,4 , 2 union all
select 4 ,5 , 1 union all
select 5 ,6 , 2;
select * from table1;
select
t1.id, t1.typ, t2.table3Id
from
table1 t1
inner join
table2 t2 on t1.id = t2.table1Id
where
t1.typ = 'myType'
order by t2.table3Id;

Related

How to get row data based on the other table row data?

I'm not sure if I write my questions title correctly, but my example would probably explain it better.
Join table_1 and table_2 and get table_1 row, if there is no is_validated= 1.
The query will get table_1, id 1 and 3, because they dont have is_validated = 1
table_1
id
1
2
3
table_2
id table_1_id is_validated
1 1 0
2 2 1
3 2 0
4 3 0
5 3 0
expected result
id
1
3
I've try to use this query, but it doesn't get the result that I want.
SELECT * FROM table_1 t1
JOIN table_2 t2 ON t1.id=t2.table_1_id AND t2.is_validated = 0
GROUP BY t2.table_1_id
Exists logic works nicely here:
SELECT t1.id
FROM table_1 t1
WHERE NOT EXISTS (SELECT 1 FROM table_2 t2
WHERE t2.table_1_id = t1.id AND t2.is_validated = 1);

how to exclude first and last row if group it on particular id

I have sample table with data like this
id uniqueid values
1 6 0
2 6 1
3 6 2
4 6 0
5 6 1
I want result like this
id uniqueid values
2 6 1
3 6 2
4 6 0
I tried like this
select id,uniqueid,values
FROM t1
WHERE
id not in(SELECT concat(MAX(message_id_pk),',',min(message_id_pk)) FROM t1
where uniqueid=6)
and `uniqueid`=6
GROUP BY uniqueid
but its not working
You can achieve the desired results by doing self join, Inner query will get the the max and min ids for per group and outer query will filter out the results by using minid and maxid
select a.*
from demo a
join (
select `uniqueid`,min(id) minid, max(id) maxid
from demo
where uniqueid=6
group by `uniqueid`
) b using(`uniqueid`)
where a.id > b.minid and a.id < b.maxid /* a.id <> b.minid and a.id <> b.maxid */
Demo
Also you can do it by using 2 sub-queries with EXISTS to exclude the min and max id of each uniqueid.
Query
select `id`, `uniqueid`, `values`
from `your_table_name` t1
where exists (
select 1 from `your_table_name` t2
where t2.`uniqueid` = t1.`uniqueid`
and t2.`id` > t1.`id`
)
and exists(
select 1 from `your_table_name` t2
where t2.`uniqueid` = t1.`uniqueid`
and t2.`id` < t1.`id`
);
Here is a sql fiddle demo
Try this -
SELECT id, uniqueid, values
FROM YOUR_TABLE
WHERE id NOT IN (MIN(id), MAX(id));

How to query update in sql based on data from other table

I Have 2 Tables.
As Example:
table1
ID, data_static1, name1
1 8 Muna
2 1 Andi
3 7 null
table2
ID, data_static2, name2
1 0 Aji
2 1 Andi
3 2 max
4 3 nadine
5 4 Rio
6 5 Panji
7 6 Eko
8 7 Pan
9 8 Muna
I want to update the column name1 in table1 based on the largest ID in table1 where table1.data_static1 is the same as table2.data_static2.
I want the results as below
table1
ID, data_static1, name
1 8 Muna
2 1 Andi
3 7 Pan
I've tried the following code
mysql> UPDATE theDB.table1 SET name1=(SELECT name2 FROM table2 WHERE data_static2=(SELECT data_static1 From table1 WHERE ID IN(SELECT MAX(ID) FROM table1))) WHERE table1.ID IN(SELECT MAX(table1.ID) FROM theDB.table1);
I get an error message
ERROR 1093 (HY000): You can't specify target table 'table1' for update in FROM clause
Simplest solution would be to use a correlated subquery for this:
update table1 t1
set name1 = (
select name2
from table2 t2
where t1.data_static1 = t2.data_static2
order by id desc limit 1
);
You can use JOIN too:
update table1 t1
join (
select *
from table2 t
join (
select data_static2,
max(id) as id
from table2
group by data_static2
) t2 using (data_static2, id)
) t2 on t1.data_static1 = t2.data_static2
set t1.name1 = t2.name2;

Mixing 3 sql queries in 1

I have 3 tables as follows
Table1
Id Name
1 abcd
2 bcd
3 dabc
Table2
Id2 Name2
2 xyz
3 def
4 mno
Table3
Id Id2 Value
1 4 1
2 3 1
3 4 1
Now,
From table1 : I have to select all Id where Name is %abc%
From table2: I have to select Id2 where Name2 is "mno"
From Table3: I have to change value to 0 from 1 where Id's value are from Table1 and Id2 is from Table2.
Table 1:
select Id from Table1 where Name like '%abc%'
Table2 :
select Id2 from Table2 where Name2 = "mno"
Table 3:
update Table3 set Value = 0 where Id in() and Id2=
But, I dont know how to make it 1 single query. Can anyone please guide me up ?
Refer to: prior stack article
You've not explained how T1 relates to T2, So I have assumed a cross join.
Whenever you have a record in T1 with name like '%abc%' (1,3) in your data..
and whenever you have a record in T2 with a name equal to 'mno' 4 then you want the value in table 3 to be 0
so the select we generate should produce
1,4
3,4
and when we inner join this back to table 3 it only selects
Id Id2 Value
1 4 1
3 4 1
Now we generate an update based on this select as outlined in the link provided above...
UPDATE table3
INNER JOIN (
SSELECT t1.ID t1ID, t2.id t2ID
FROM table1 t1
CROSS JOIN table2
WHERE t1.name like '%abc%'
and t2.name like = 'mno') B
on B.t1ID = t3.Id
and B.t2ID = T3.ID2
SET value = 0
Giving us a result of
Id Id2 Value
1 4 0
2 3 1
3 4 0
if we select * from table3
update t3
set t3.Value = 0
from Table3 t3
inner join Table1 t1
on t3.Id = t1.Id
inner join Table2 t2
on t3.Id2 = t2.Id2
where t1.Name like '%abc%' and t2.Name2 = 'mno'
OR
update Table3
set value = 0
where Id in (select Id from Table1 where Name like '%abc%')
and Id2 in (select Id2 from Table2 where Name2 = 'mno')
You should think about UPDATE ... WHERE EXISTS as follows:
update Table3 set Value = 0
WHERE EXISTS (SELECT 1 FROM Table1 where Name LIKE '%abc%' AND Table1.Id=Table3.Id )
AND EXISTS (SELECT 1 FROM Table2 where Name2 = "mno" AND Table2.Id2=Table3.Id2)

How to select a column from one table and two columns from another table when both the tables have no relation in T-SQL?

I will explain my problem as simple as possible.
I have written a select query Query1 on Table1 which gives the me the following result
SELECT * FROM Table1 WHERE TypeID=1
ID Column1 Column2 TypeID
1 A A 1
2 B B 1
3 C C 1
I have another table Table2 which has data in the following format
ID Column1 Column2
1 0 0
1 1 1
2 2 2
2 3 3
2 4 4
3 5 5
3 6 6
I have written a another select query Query2 on Table1 which gives the me the following result
SELECT * FROM Table1 WHERE TypeID=2
ID Column1 Column2 TypeID
4 A A 2
5 B B 2
6 C C 2
Table1 and Table2 have no column in common and data in Column1 and Column2 for both TypeID's in Table1 is same and currently data in Table2 has data with values of ID column from Table1 for id's 1,2,3 only and i want to write a select query to select same data from Table2 but with values of ID column from Table1 with TypeID as 2 which I have given below
ID Column1 Column2
4 0 0
4 1 1
5 2 2
5 3 3
5 4 4
6 5 5
6 6 6
how can I achieve this by writing a select query in sql server?
select T1.column1, isnull(T2.column2,0) as column2,
isnull(T2.column3,0) as column3
from table1 T1
left outer join on Table2 T2 on T1.column9=T2.column9
where ....
If I understood the question correctly, the query whould be like that:
SELECT
T_ID.T2_ID,
Column1,
Column2
FROM
Table2
JOIN
(
SELECT
T1.ID AS T1_ID,
T2.ID AS T2_ID
FROM
Table1 AS T1
JOIN Table1 AS T2 ON T1.Column1 = T2.Column1 AND T1.Column2 = T2.Column2
WHERE
T1.TypeID = 1
AND T2.TypeID = 2
) AS T_ID ON Table2.ID = T_ID.T1_ID