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;
Related
I've two tables like this:
First :
id
num
1
a
2
b
3
c
Second:
id
first_id
value
11
1
a1
12
1
a2
13
1
a3
And I need to get result like this:
id
value
1
a1-a2-a3
I've tried with query:
SELECT first.id, (SELECT second.value FROM second
WHERE second.first_id = first.id) AS value
FROM first
But I've got #1242 error. How I can do it?
You can use group_concat()
select first_id as id,
group_concat(`value` order by `value` separator '-') as combined_values
FROM second_table
group by first_id
I need to count matches in a database.
Input:
id_to id_from
1 2
2 1
1 3
3 1
1 4
5 1
the 5th and 6th row has only one direction so doesn't count
Sample Output:
id_match
1
2
3
So, for 1 (implicit), 2 and 3 there is a reverse match but for 4 and 5 there aren't.
---- EDITED ----
Supposing the table name is "example" and I want to get all matches of id=1 then the SQL query will be:
SELECT count(*) FROM
(SELECT id_to FROM example WHERE id_from = 1) as t1,
(SELECT id_from FROM example WHERE id_to = 1) as t2
WHERE t1.id_to = t2.id_from
but maybe there is a better way to do it
You could try
SELECT DISTINCT id_from AS matched_id
FROM your_table AS data1
WHERE EXISTS (SELECT 1
FROM your_table AS data2
WHERE data1.id_from = data2.id_to
AND data1.id_to = data2.id_from)
I've created a demo here
I have following type of table.I want to output of last record(most recent) of particular group.Please suggest My sql query.
Id Name random number
-------------------------
1 A 1233
2 A 1778
3 A 1221
4 B 1298
5 B 1289
6 C 1267
I want a last record of group A
e.g.
ID Name Random number
----------------------
3 A 1221
select id, name, random from table where Name='A' order by id desc limit 1
Here is query :
select * from tbl where id IN (select max(id) from tbl group by name);
And here is fiddle: http://sqlfiddle.com/#!2/01d69/8
SELECT * From Table1 Where [Id] in (
SELECT Max([Id]) as [maxId] From Table1 Where [Name] = 'A')
Fiddle
how can I select all from a table, and if there are identical values of column name then only select the row that has the greatest id value so If there was a table like this:
id name age country
---+------+---+-------
1 bob 24 UK
2 john 48 USA
3 janet 72 USSR
4 bob 96 Ukraine
it would only select the 'bob' with the highest id so the result would return:
id name age country
---+------+---+-------
2 john 48 USA
3 janet 72 USSR
4 bob 96 Ukraine
Thank you.
Try this query
select * from table_name where ID in(select MAX(ID) from table_name group by name)
You could use a subquery to calculate the maximum ID for every name, and then return all rows that matches the IDs returned by the subquery:
SELECT *
FROM People
WHERE id IN (SELECT MAX(id) FROM People GROUP BY Name)
Please see fiddle here.
You could use a not exists subquery that filters out rows with with the same name and a greater id:
select *
from People p1
where not exists
(
select *
from People p2
where p1.Name = p2.Name
and p2.Id > p1.Id
)
you can do it this way :
Select Table_1.* from table_1 inner join (
Select Max(ID) as ID from Table_1 Group by ID) x On Table.Id on x.ID
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