I have duplicate data on my table as described bellow.
no name adrress
1 Joe No.3
2 Joe No.2
3 Joe No.1
4 Anna No.4
5 Anna No.5
6 Ali No.6
I want to show only the first item from duplicate data like bellow.
no name address
1 Joe No.3
2 Anna No.4
If you mean to find the duplicate only, this one similar to your question here
for your case will be like this
SELECT x.*
FROM new_table x
JOIN
( SELECT name
, MIN(id) as min_id , COUNT(id) as count_id
FROM new_table
GROUP
BY name
) y
ON y.name = x.name
AND y.min_id = x.id
AND y.count_id > 1
ORDER
BY id;
hope that answer your question.
There could be lot of answers to this question see which one fits your requirements:
Select * from tablename where no=1;
Select * from tablename where adrress LIKE "%3%";
Select * from tablename ORDER BY no LIMIT 1;
You can use this query for selecting particular row in the table
select * from yourtablename WHERE primarykeyfield=" ";
Try this:
mysql_query("select * from $table where id='1';");
Related
I have a table like this:
name |id | state
name1 12 4
name1 12 4
name2 33 3
name2 33 4
...
I want to select every name and id from table where state is only 4, that means name1 is correct, because it only has two records with state 4 and nothing more. Meanwhile name2 is wrong, because it has record with state 4 and record with state 3.
You can use aggregation as shown below:
SELECT name, id
FROM your_table
GROUP BY name, id
HAVING SUM(state<>4)=0;
See a Demo on SQL Fiddle.
select name, id from mytable where id not in
(select distinct id from mytable where state <> 4)
you might need 2 sub queries .
select with group by name were state 4
select with group by name
compare the count if the count is same then select it
example : select name , count (name) from table where state = 4 as T1
select name , count (name) from table as T2
select T1.name from T1 and T2 where T2.count = T1.count
You can use not exists like this:
select distinct name, id
from table1 a
where not exists (select *
from table1 b
where a.id=b.id and state<>4)
In a more general case you can use count distinct (with not exists or with a join):
select distinct name, id
from table1 a
where not exists (
select *
from table1 b
where a.id=b.id
group by id
HAVING count(distinct state)>1)
id user_id animal
1 12 Cat
2 5 Lion
3 12 Snake
how do I select any one or the first one of user_id = 12. Please note: user_id 12 appears more than once in the table, I just need to select any one of them.
Since you say first or any, you can use limit.
select * from table where user_id=12 limit 1;
you need one for each user and other and you don't matter the othres row for the same user_id you can use
select *
from my_table
where (id, user_id) in (select min(id), user_id
from my_table
group by user_id)
========================================================
this is the sample db
I just want to get user who has both 2 and 14 in skills column. The answer should be "2"
Try this:
SELECT seekerID
FROM mytable
WHERE skillID IN (2, 14)
GROUP BY seekerID
HAVING COUNT(DISTINCT skillID) = 2
DISTINCT keyword is necessary only in case skillID values can occur multiple times for a single seekerID.
The easiest way to do this would be
select seekerID, count(*) as cnt
from table_name
where skillid in (2,14)
group by seekerID
having cnt = 2
use this:
select seekerID from table_name where skillid="2" and seekerID = ( select author from table_name where skillid="14")
I would like to have a mysql select query that will select all from a table and also include a count of duplicates that exist in the table for a specific field. Here is a table and the results I would like.
TestTable
Id Name Dogs
1 Eric 1
2 Dave 2
3 Chris 4
4 Eric 3
I would like to have the following results returned, based on a search for records that have duplicate names.
Id Name Dogs
1 Eric 1
4 Eric 3
select * from your_table
where name in
(
select name
from your_table
group by name
having count(*) > 1
)
Try This
SELECT
a.Id,a.Name,a.Dogs,
(SELECT count(b.Name)
FROM TestTable b
WHERE b.Name=a.Name) as totcount
FROM TestTable a GROUP By a.Name;
This will work.
I have a table student like this
id | name | zip
1 | abc | 1234
2 | xyz | 4321
3 | asd | 1234
I want to get all records but zip code should not be repeated. So In case of above table records, record No 1 and 2 should be fetched. Record No. 3 will not be fetched because it has a zip code which is already in record No. 1
SELECT DISTINCT fieldName FROM tableName;
The following query will only select distinct 'zip' field.
SELECT DISTINCT zip FROM student;
SELECT * FROM tableName GROUP BY fieldName;
The following query will select all fields along with distinct zip field.
SELECT * FROM student GROUP BY zip;
TRY
SELECT DISTINCT(zip),id,name FROM student;
OR
SELECT * FROM student GROUP BY zip;
Altough in MySQL you can get away with:
SELECT *
FROM student
GROUP BY zip
I would choose:
SELECT *
FROM student t
JOIN
( SELECT MIN(id) AS minid
FROM student
GROUP BY zip
) AS grp
ON grp.minid = t.id
Since presumably the other columns are of some interest....
SELECT y.*
FROM yourTable y,
(SELECT MIN(y2.id)
FROM yourTable y2
GROUP BY y2.zip) ilv
WHERE ilv.id=y.id;
(or you could use the max-concat trick)
update
Oracle have now removed the max concat trick from the linked page - but it is described elsewhere on the internet
Try Using
Select Distinct(zip),id,name group by zip;
Is there any problem if I use as this below?
select distinct zip,name,id from student;
select id, name, distinct(zip) from student;