How to eliminate a specific row in mysql - mysql

id manager_id name
1 1 Tony
2 2 smith
3 2 harry
4 1 jack
5 1 william
6 2 steve
7 2 no name
8 2 john
9 2 no name
I used this query for get same last ordered id.
select t.*
from tablename t
where not exists (select 1
from tablename
where manager_id <> t.manager_id
and id > t.id)
| id | manager_id | name |
| --- | ---------- | ----- |
| 6 | 2 | steve |
| 7 | 2 | no name |
| 8 | 2 | john |
| 9 | 2 | no name |
The above query is working fine. but now my need is Eliminating the specific rows where name = no name. So how to customize the query.

Please use below query to eliminate no name,
select t.*
from tablename t
where not exists (select 1
from tablename
where manager_id <> t.manager_id
and id > t.id) and t.name != 'no name';

You could use a delete with inner join based on your query
delete t1 from tablename t1
inner join (
select t.*
from tablename t
where not exists (
select 1
from tablename where manager_id <> t.manager_id and id > t.id
)
) t2 on t1.id = t2.id
and t2.name ='no name'
or based on your comment could be you just want set the value to null
in this case you need update
Update tablename t1
inner join (
select t.*
from tablename t
where not exists (select 1 from tablename where manager_id <> t.manager_id and id > t.id)
) t2 on t1.id = t2.id
and t2.name = 'no name'
set t2.name = NULL

Related

How can I get a record which has two corresponding columns which are multiple in SQL?

For example, I have two tables:
ID | Name
------------
1 | test 1
2 | test 2
ID2| ID | Age
--------------
1 | 1 | 18
2 | 1 | 18
3 | 1 | 19
4 | 2 | 18
5 | 2 | 19
I want to have all records that have columns which are multiple in name with age but I don't know how to do that.
I want an output like this:
Name | Age
--------------------
test 1 | 18
test 1 | 18
Can anyone help me?
Try following query:
Select t1.*, t2.*
from table1 t1
join table2 t2
on t1.id = t2.id
join (select id, age
from table2
group by id, age
having count(*) > 1
) t3
on t1.id = t2.id and t2.id = t3.id and t2.age = t3.age
Use exists:
select t.*
from t
where exists (select 1
from t t2
where t2.name = t.name and t2.age = t.age and
t2.id <> t.id
);
With an index on (name, age, id), this should be the fastest approach.
You can also use an IN on tupples.
And a GROUP BY can be combined with a HAVING to only get those that have duplicate (name, age).
SELECT t1.Name, t2.Age
FROM YourTable2 t2
LEFT JOIN YourTable1 t1 ON t1.ID = t2.ID
WHERE (t2.ID, t2.Age) IN (
SELECT ID, Age
FROM YourTable2
GROUP BY ID, Age
HAVING COUNT(*) > 1
);

Create a Join leaving one row for any possible duplicate

I have a 2 tables that looks like this
How can I call the same column wihout duplicating it
TYSM for help
Please try below mentioned Query:
select distinct t2.data,t1.key,t1.data from t1.table1 JOIN table as t2 ON t1.key = t2.key
You could assign a row number using a variable to t2 then join to t1 supressing the output of the t1.key.
for example
drop table if exists t1,t2;
create table t1 (id int);
create table t2 (id int, name varchar(2));
insert into t1 values(1),(2),(3),(4);
insert into t2 values(1,'s1'),(1,'s2'),(2,'s3'),(3,'s4'),(4,'s5');
select s.id, s.name,
case when s.rn = 1 then s.rn
else ''
end as something
from t1
join
(
select t2.id,t2.name,
if(t2.id <> #p, #rn:=1,#rn:=#rn+1) rn,
#p:=t2.id
from t2,(select #rn:=0,#p:=0) r
) s on t1.id = s.id
order by t1.id, s.name
Result
+------+------+-----------+
| id | name | something |
+------+------+-----------+
| 1 | s1 | 1 |
| 1 | s2 | |
| 2 | s3 | 1 |
| 3 | s4 | 1 |
| 4 | s5 | 1 |
+------+------+-----------+
5 rows in set (0.00 sec)

Count records in one table where there are no records in linked table matching certain criteria

I have 2 tables:
T1:
id | name
------ | ------
1 | Bob
2 | John
3 | Joe
T2:
id | T1_id | type
------ | ------ | ------
1 | 1 | call
2 | 1 | email
3 | 1 | fax
4 | 2 | call
5 | 2 | email
6 | 2 | fax
7 | 3 | call
8 | 3 | email
I want to count the number of records in T1 which do not have a record in T2 with a type of 'fax'.
So the answer in this case would be 1 (3|Joe)
Currently I have:
SELECT count(*)
FROM `T1`
JOIN `T2` on `T1`.`id` = `T2`.`T1_id`
WHERE `T2`.`type` != 'fax'
But this is obviously counting all the records which are not 'fax'. I just cant get the logic in my head.
Any help would be appreciated!
A subquery is unnecessary:
SELECT COUNT(DISTINCT t1.id)
FROM t1
LEFT
JOIN t2
ON t2.t1_id = t1.id
AND t2.type = 'fax'
WHERE t2.id IS NULL;
select count(*)
from
(
SELECT t1.id
FROM T1
LEFT JOIN T2 on T1.id = T2.T1_id
GROUP BY t1.id
HAVING sum(T2.type = 'fax') = 0
) tmp
The answers given by Strawberry and juergen d are correct, but for completeness, here's another example using NOT EXISTS. All the queries will have different execution plans, so depending on your data in T1 and T2 YMMV:
SELECT COUNT(*)
FROM `T1`
WHERE NOT EXISTS (
SELECT *
FROM `T2`
WHERE `T2`.`T1_id` = `T1`.`id`
AND `T2`.`type` = 'fax'
)

How to show parent record Id in child record column-Mysql

I want to show my parent id with child record(duplicate record). Here is my table
ID|Name |Comments|
__|_____|________|_
1 |Test1|Unique |
2 |Test2|Unique |
3 |Test1|Unique |
4 |Test2|Unique |
5 |Test1|Unique |
6 |Test3|Unique |
Expected Result:
ID|Name |Comments |
__|_____|__________________|_
1 |Test1|Unique |
2 |Test2|Unique |
3 |Test1|Duplicate with: 1 |
4 |Test2|Duplicate with: 2 |
5 |Test1|Duplicate with: 1 |
6 |Test3|Unique |
not sure what the exact goal here, but here is a single query that get the job done:
mysql> select ID,tbl.Name,if(no!=ID,concat('Duplicate with: ',no),'Unique') Comments from tbl left join (select ID no,Name from tbl group by Name) T on T.Name=tbl.Name;
+----+-------+-------------------+
| ID | Name | Comments |
+----+-------+-------------------+
| 1 | Test1 | Unique |
| 2 | Test2 | Unique |
| 3 | Test1 | Duplicate with: 1 |
| 4 | Test2 | Duplicate with: 2 |
| 5 | Test1 | Duplicate with: 1 |
| 6 | Test3 | Unique |
+----+-------+-------------------+
Check This Live Demo using 'coalesce' and 'Case when'
Query :
select id
,name
,coalesce(
( select coalesce(case when min(id)>0 then concat('Duplicate with : ',min(id)) else null end,Comments)
from Yourtable t2 where t2.name = t.name and t2.id < t.id group by Comments)
,Comments) as Comments
from Yourtable t
order by id
Output :
hey you can try this query and it is giving expected result.
select t1.id,t1.`name`,
CASE WHEN (select count(`name`) from table_name t2 where t2.`name` = t1.`name` and t2.id <= t1.id ) = 1
then 'unique'
else CONCAT('Duplicate with :',(select min(t3.id) from table_name t3 where t3.name = t1.`name`))
end as 'comments'
from table_name t1
replace table_name with your table.
Hope this works for you. Ask if any doubt
Using only a single sub-query.
select id
,name
,coalesce
(
concat
(
'Duplicate with: '
,(select min(id) from mytable t2 where t2.name = t.name and t2.id < t.id)
)
,'Unique'
) as Comments
from mytable t
order by id

Returns all rows which have same column value based on other column value

Consider I have the following table:
Id | sid | email
___________________________________________________
1 | 10 | john#yahoo.com
2 | 11 | elsa#gmail.com
3 | 10 | johnconnor#gmail.com
4 | 10 | john.smith#gmail.com
5 | 12 | ninjamutant#yahoo.com
I would like to query all rows which have same "sid" by passing known "email"
So if I pass the email as "john.smith#gmail.com", it should return rows with id number 1, 3, and 4.
Try this :
select * from yourtable a
inner join (
select sid
from yourtable
where email = "john.smith#gmail.com"
) b on b.sid = a.sid
select T2.*
from my_table T1 join my_table T2
on T1.sid = T2.sid
where T1.email = 'xxx'