so i have a database table like:
NAME name_ID
---------------
Joao 1
Maria 3
Joao 1
carlos 2
carlos 2
i want to do a select query that displays all duplicates only like this :
NAME name_ID
---------------
Joao 1
Joao 1
carlos 2
carlos 2
and other select query that displays singles like so :
NAME name_ID
---------------
Maria 3
This would be simpler if you had a unique id column in each table. I encourage you to design tables with primary keys.
In any case, you can do this with a query of the form for duplicates:
select t.*
from databasetable t join
(select name, count(*) as cnt
from databasetable
group by name
) tt
on t.name = tt.name
where cnt > 1;
For singletons, the comparison would be cnt = 1.
EDIT:
With a unique id and an index on (name, id), the following is probably faster for duplicates:
select t.*
from databasetabe t
where exists (select 1
from databasetable t2
where t2.name = t.name and t2.id <> t.id
);
Singletons would use not exists instead.
select name from table group by name haveing count(*)=1 ###for Maria
select * from table where not in (previous select) order by name
Related
I have a table with 100 000 record, I want to select only the none repeated.
In another word, if the row are duplicated did not show it at all
ID Name Reslut
1 Adam 10
2 Mark 10
3 Mark 10
result
ID Name Reslut
1 Adam 10
any ideas ?
You could join a query on the table with a query that groups by the name only returns the unique names:
SELECT *
FROM mytable t
JOIN (SELECT name
FROM mytable
GROUP BY name
HAVING COUNT(*) = 1) s ON t.name = s.name
Using the same set :
ID Name Result
1 Adam 10
2 Mark 10
3 Mark 10
4 Mark 20
I'm guessing the final solution would be:
ID Name Result
1 Adam 10
4 Mark 20
Using the above query previously suggested I modified it to take the result into consideration:
SELECT t1.*
FROM myTable t1
JOIN
(
SELECT name, result
FROM myTable
GROUP BY name, result
HAVING COUNT(*) = 1
) t2
WHERE
t1.name=t2.name and
t1.result = t2.result;
Sorry I don't really know how to make a title for this because I can't explain it really. for example i have here a table
c_id emp_id clinic_id
1 1 1
2 1 2
3 2 1
4 3 3
5 1 3
now i will do a query like this
select distinct * from table where clinic_id <> 1
And the result would be
c_id emp_id clinic_id
2 1 2
4 3 3
5 1 3
at this point I need help, if from the where clinic_id <> 1 a certain emp_id is within its row of condition(Sorry for my bad english). for example emp_id 1. All emp_id 1 must not be display also.
So the result would be just
c_id emp_id clinic_id
4 3 3 // *The result I want*
You can use NOT EXISTS for this:
select distinct *
from mytable as t1
where clinic_id <> 1 and
not exists (select 1
from mytable as t2
where t1.emp_id = t2.emp_id and t2.clinic_id = 1)
For the result you're looking for, wouldn't something like this be simpler?
SELECT DISTINCT * FROM table WHERE clinic_id !=1 AND emp_id !=1
Here we're saying we want any clinic_id that is not 1 and any emp_id that is also not 1.
Since you're dealing with PHP, then you would simply substitute the numbers for the variables you're trying to not match.
SELECT DISTINCT * FROM table WHERE clinic_id !=$session_variable AND emp_id !=$some_value
SELECT DISTINCT *
FROM table
WHERE clinic_id <> 1
AND emp_id NOT IN
(SELECT DISTINCT emp_id
FROM table
WHERE clinic_id = 1)
Try this one.
It uses the subquery to return the emp_ids which are in the same row as the 1 in the column clinic_id, and removes them from the resultset because you also don't want those emo_ids.
Also you could use a GROUP BY clause instead of DISTINCT. Usually GROUP BY would be turned into a distinct by the database if you are not using any aggregate functions, but sometimes they behave differently. If you are interested in this topic you can also see this question: Is there any difference between GROUP BY and DISTINCT
SELECT c_id, emp_id, clinic_id
FROM clinics
WHERE clinic_id <> 1
AND emp_id NOT IN
(SELECT DISTINCT emp_id
FROM clinics
WHERE clinic_id = 1)
GROUP BY c_id, emp_id, clinic_id;
TABLE 1 TABLE 2
id name mob id course mark
1 joe 0000 1 English 77
2 john 0000 2 maths 89
I need to show the name of the person from table 1 who has the MAX(grade) in table 2 using a nested query.
SELECT t1.name
FROM t1
WHERE t1.id = t2.id = (
SELECT id
FROM t2
WHERE mark =
(
SELECT MAX(mark)
FROM t2
)
);
Well, this satisfies the brief ;-):
SELECT a.*
FROM table_a a
JOIN (SELECT * FROM table_b) b
ON b.id = a.id
ORDER
BY mark DESC
LIMIT 1;
I have a query that returns data in the following format:
id | name | number
1 John 12545
1 John 50496
2 Mary 23443
3 Mark 54
3 Mark 5600
3 Mark 50206
I would like to find out the number of distinct ids that appear in the result set. For example, for the result above. I would like to obtain the value 3.
Is there any way to add a column so the result looks like this instead?
count | id | name | number
3 1 John 12545
3 1 John 50496
3 2 Mary 23443
3 3 Mark 54
3 3 Mark 5600
3 3 Mark 50206
My query is:
SELECT * FROM (
SELECT id FROM tableA
WHERE xyz
) as t1
JOIN tableB using (id)
SELECT (SELECT COUNT(DISTINCT id) FROM tableName) totalCount,
id,name,number
FROM tableName
or by using CROSS JOIN
SELECT x.totalCount,
a.id, a.name, a.number
FROM tableName a, (SELECT COUNT(DISTINCT id) totalCount
FROM tableName) x
You should try :
SELECT id,name,number, (SELECT COUNT(DISTINCT name) FROM YourTableName) FROM YourTableName
Good luck
SELECT COUNT(DISTINCT id) would be faster than using column name.
SELECT (SELECT COUNT(DISTINCT id) FROM tableName) as 'count',
id,name,number
FROM tableName
SELECT COUNT(id) AS count , id, name, number
FROM
(
SELECT id
FROM tableA
WHERE xyz
) as t1
JOIN tableB using (id)
GROUP BY id, name, number
I have a table where I can have multiple names for a given id like this:
a_table (id int, name varchar(100), priority int);
I need a query that will search on names but make sure it will return only 1 name for each id, and that name will be the one with the higher priority.
e.g. if my data are
1, AaaB, 2
1, AbbB, 1
1, AccB, 0
2, foo, 0
3, AddC, 0
I want my query for "A%" to return:
1, AaaB
3, AddC
I was thinking something like:
select * from a_table where name like 'A%' group by id;
But this will not guarantee that the value with the higher priority will be selected.
Any ideas?
I believe you want what the MySQL documentation calls the rows holding the group-wise maximum of a certain column:
For the task "For each article, find the dealer or dealers with the most expensive price":
SELECT article, dealer, price
FROM shop s1
WHERE price=(SELECT MAX(s2.price)
FROM shop s2
WHERE s1.article = s2.article)
ORDER BY article;
+---------+--------+-------+
| article | dealer | price |
+---------+--------+-------+
| 0001 | B | 3.99 |
| 0002 | A | 10.99 |
| 0003 | C | 1.69 |
| 0004 | D | 19.95 |
+---------+--------+-------+
You have to first get the highest priority per id and then filter on the names:
select t2.id, t2.name, t2.price
from (
select id, max(priority)
from a_table
group by id
) t1,
a_table t2
where t1.id = t2.id
and t1.priority = t2.priority
and t2.name like 'A%'
Taking #niktrs's valid suggestion, this is the same above query using the standard JOIN instead of where for joining tables. This is more preferred over the previous one
select t2.id, t2.name, t2.price
from (
select id, max(priority)
from a_table
group by id
) t1 JOIN a_table t2 ON t1.id = t2.id
and t1.priority = t2.priority
and t2.name like 'A%'
select *
from a_table t
join (
select max(Priority) MaxPriority, Name
from a_table a
where name like 'A%'
group by Name
)x where x.MaxPriority=a.Priority and x.Name=t.Name
On the basis the first column in the data example is the "Priority".
This is just the SQL linked from Alvaro's answer though.
select id, name
from a_table at
where
name like 'A%' and
priority = (
select max(priority)
from a_table
where (id = at.id) and (name like 'A%')
)
would this work out,
select distinct id,first_value(name)over(partition by id order by name)
from demo_tab t
where t.name like 'A%'
Sorry pratik,
it must have been
select distinct id,first_value(name)over(partition by id order by priority desc)
from demo_tab where name like 'A%'