MySQL sly select - mysql

Can not understand how to make an select:
table structure:
id name value date
1 ivan 2 2010-01-01
2 ivan 3 2010-05-08
3 ivan 1 2009-04-14
4 sasha 2 2011-11-11
5 sasha 9 2012-04-04
How to get?:
name value(in last time)
ivan 3
sasha 9

This is untested, but should work:
SELECT
name,
MAX(value)
FROM
structure
GROUP BY
name

This query should work
select f.name, f.value, f.date
from (
select id,name,max(date) as dat from tableName group by name
) as x inner join tableName as f on f.name = x.name and f.date = x.dat;

SELECT DISTINCT name, value FROM table ORDER BY date DESC
thats what i understand from your current description so far.

Something like this:
SELECT
MAX(value),
name
FROM
Table
GROUP BY
name

Related

How to get number of same id's where the item was found in mysql?

Let's say I have table like this:
some_id
date
1
2022-02-01
2
2022-02-02
3
2022-02-03
3
2022-02-04
3
2022-02-05
3
2022-02-06
I want to get the number of rows based on the id where the date was found?
I tried this but it's not working:
SELECT COUNT(id) FROM dates WHERE date = '2022-02-04'
Expected output should be 4 rows since there are 4 same id's where the 2022-02-04 was found.
This should do the job:
SELECT COUNT(*) FROM tbl
WHERE id IN (
SELECT id FROM tbl WHERE `date`='2022-02-04'
)
An exists query should do it:
SELECT id, COUNT(*)
FROM t
WHERE EXISTS (
SELECT 1
FROM t AS x
WHERE x.id = t.id
AND x.date = '2022-02-04'
)
GROUP BY id
Using exists logic we can try:
SELECT COUNT(*)
FROM dates d1
WHERE EXISTS (SELECT 1 FROM dates d2
WHERE d2.some_id = d1.some_id AND
d2.date = '2022-02-04');

MySql , How to select the not repeated rows only

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;

Find duplicate records in MySQL without named column

I have a table like this:
**lead_id** **form_id** **field_number** **value**
1 2 1 Richard
1 2 2 Garriot
2 2 1 Hellen
2 2 2 Garriot
3 2 1 Richard
3 2 2 Douglas
4 2 1 Tomas
4 2 2 Anderson
Where field_number = 1 is the name and field_number = 2 is the surname.
I would like to find entries that are equal by name OR surname and group them by lead_id, so the output could be like this:
1
2
3
Any thoughts on how this can be done?
This should work and be reasonably efficient (depending upon indexes):
select distinct lead_id
from tablename as t1
where exists (
select 1
from tablename as t2
where t1.field_number = t2.field_number
and t1.value = t2.value
and t1.lead_id <> t2.lead_id
)
Select leadid from (
Select DISTINCT leadid,value from tablename
Where fieldnumber=1
Group by leadid,value
Having count(value) >1
Union all
Select DISTINCT leadid,value from tablename
Where fieldnumber=2
Group by leadid,value
Having count(value) >1
) as temp
Surely there is a faster option

Counting unique numbers in a column MySQL

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

mysql select distinct where clause

I have this table, and I want to select all distinct user_id values where there is both an event='AAAAAAAAAA' and event ='XXXXXXXXXX' (i.e. only result should be user_id=123456789)
unique_row_id user_id event event_timestamp
----------------------------------------------------------------
0 123456789 AAAAAAAAAA 2010-01-20 15:00:00
1 123456789 abcdefghij 2010-01-20 15:00:05
2 123456789 XXXXXXXXXX 2010-01-20 15:00:15
3 987654321 AAAAAAAAAA 2010-01-20 16:00:00
4 987654321 abcdefghij 2010-01-20 16:00:05
5 987654321 abcdefghij 2010-01-20 16:00:15
6 111111111 XXXXXXXXXX 2010-01-20 16:01:00
7 111111111 XXXXXXXXXX 2010-01-20 16:01:05
8 111111111 XXXXXXXXXX 2010-01-20 16:01:15
I've tried both:
SELECT distinct user_id from mydata where event='AAAAAAAAAA' and event='XXXXXXXXXX'
which gives me an empty result set, and
SELECT distinct user_id from mydata where event='AAAAAAAAAA' or event='XXXXXXXXXX'
which gives me everything. Any suggestions - I'm guessing I need to go down the 'join' road here do I?
Try this:
select user_id from mydata where event='AAAAAAAAAAA'
union
select user_id from mydata where event='XXXXXXXXXXX';
This will select both lists (users w/ AAA, users w/ XXX), and then merge them together, removing duplicate rows.
Hope that helps!
Thanks,
Joe
I'd do it with a subquery:
select distinct user_id
from mydata m
where event = 'AAAAAAAAAA'
and exists (
select 1
from mydata
where user_id = m.user_id
and event = 'XXXXXXXXXX'
);
The conditions on the WHERE clause are only applicable on a single row at a time.
In order to check two rows, you have to use JOIN:
SELECT distinct a.user_id
FROM mydata a, mydata b
WHERE a.event='AAAAAAAAAA' AND b.event='XXXXXXXXXX' AND a.user_id = b.user_id
If you know that an event can only appear once per user, you can use GROUP BY:
SELECT user_id
FROM mydata
WHERE event in ('AAAAAAAAAA', 'XXXXXXXXXX')
GROUP BY user_id
HAVING count(user_id)=2
SELECT
DISTINCT user_id
FROM
my_table AS outer_table
WHERE
EXISTS (
SELECT
1
FROM
my_table AS inner_table_A
WHERE
inner_table_A.user_id = outer_table.user_id AND
inner_table_A.event = 'AAAAAAAAAA'
) AND
EXISTS (
SELECT
1
FROM
my_table AS inner_table_B
WHERE
inner_table_B.user_id = outer_table.user_id AND
inner_table_B.event = 'XXXXXXXXXX'
)
I would use something like this...
SET #events := 'AAAAAAAAAA,XXXXXXXXXX';
SELECT user_id
FROM mydata
GROUP BY user_id
HAVING GROUP_CONCAT(DISTINCT event ORDER BY event SEPARATOR ',') = #events;