On this solution the user is querying on id to display other columns in the select but query only distinct on a single column. I've tried to do this but can't get the syntax right. Here is the query but I only want distinct on DevelopmentDisplay column.
SELECT DISTINCT
`RESI`.`DevelopmentDisplay`,
`RESI`.`City`,
`RESI`.`ZipCode`
FROM
`RESI`
WHERE
`RESI`.`ZipCode` = '91263'
ORDER BY
`RESI`.`DevelopmentDisplay` ASC
Yes you can self join tables.
I see two options like this. However i would suggest you index DevelopmentDisplay column. Depending on the number of records it can get very slow.
SELECT
t1.DevelopmentDisplay,
t1.City,
t1.ZipCode
FROM
RESI t1,
(SELECT DISTINCT DevelopmentDisplay FROM RESI) t2
WHERE
t1.ZipCode = '91263' AND
t1.DevelopmentDisplay = t2.DevelopmentDisplay;
Alternatively:
SELECT
t1.DevelopmentDisplay,
t1.City,
t1.ZipCode
FROM
RESI t1,
WHERE
t1.ZipCode = '91263' AND
t1.DevelopmentDisplay IN (SELECT DISTINCT DevelopmentDisplay FROM RESI);
Related
I have a query,
SELECT t2.id, t1.image, SUBSTRING(t2.start_time,1,10) AS mytime,
t2.user
FROM post_table t1
INNER JOIN watchUserList t2 ON t1.id = t2.movie_id
WHERE user = 'john#gmail.com'
ORDER BY id DESC;
In this query I want to fetch DISTINCT of mytime. I tried DISTINCT(SUBSTRING(t2.start_time,1,10)) AS mytime and SUBSTRING(t2.start_time,1,10) AS DISTINCT(mytime). But both doesn't work. How to get Distinct of a Substring in MySQL. Is there any way?
The correct syntax is
SELECT DISTINCT t2.id, t1.image, SUBSTRING(t2.start_time,1,10) AS mytime,
t2.user
FROM post_table t1
INNER JOIN watchUserList t2 ON t1.id = t2.movie_id
WHERE user = 'john#gmail.com'
ORDER BY id DESC;
But note, that its distinct over all the fields in the field-list.
Distinct on Multiple Columns
When we use MySQL Distinct on multiple columns, then the SELECT Statement writes the unique combination of multiple columns instead of unique individual records.
Distinct On Multiple Columns
I have table 1 which contains unique values and table 2 which contains multiple values for the same email. What I want to do is select the first value of 'id' - table 2 contains a number of ids and matching emails
SELECT DISTINCT
table1.email,
table2.id FROM
table1
INNER JOIN users ON table1.email = table2.Email
the problem is the output needs to be unique - i.e. one ID - the first one from table2 that is associated with a given email - currently we're getting multiple results - no unique or distinct values.
Probably
any id - add ORDER BY RAND()
1 row of result - add LIMIT 1.
So, the query might be something like this:
SELECT DISTINCT
table1.email,
table2.id
FROM table1
INNER JOIN table2 ON table1.email = table2.email
ORDER BY RAND()
LIMIT 1
Based on new information, it seems like you're looking for something like this instead:
Note: Works on MySQL v8+ and MariaDB 10.2+ that have window function:
SELECT email, id
FROM
(SELECT table1.email,
table2.id,
ROW_NUMBER()
OVER
(PARTITION BY table1.email ORDER BY table2.id) AS 'RowNumber'
FROM table1
INNER JOIN table2 ON table1.email = table2.email) t
WHERE RowNumber=1;
Assign ROW_NUMBER() with table1.email as partition and sort by table2.id ascending (note that the default sorting of ORDER BY is ascending so there's no need to define it as ORDER BY xxx ASC).
Turn the base query into a subquery then do a SELECT .. with condition of WHERE RowNumber=1. Hence, it will return only a single row for each email.
Alternatively, depending on your data, you could just simply do something like this:
SELECT table1.email,
MIN(table2.id) AS minID
FROM table1
INNER JOIN table2 ON table1.email = table2.email
GROUP BY table1.email;
Demo fiddle
the sql as follows come from mysql document. it is:
SELECT * FROM t1 AS t
WHERE 2 = (SELECT COUNT(*) FROM t1 WHERE t1.id = t.id);
The document say It finds all rows in table t1 containing a value that occurs twice in a given column , and doesnot explain the sql.
t1 and t is the same table, so the
count(*) in subquery == select count(*) from t
, isn't it?
count(*) in subquery == select count(*) from t
is wrong. because in mysql you can't use it like that. so you have to run it like that to get result of same id having two rows.
if you want to get count of same occurrence,
SELECT id, name, count(*) AS all_count FROM t1 GROUP BY id HAVING all_count > 1 ORDER BY all_count DESC
And also you can get values as your query like this as well,
select * from t1 where id in ( select id from t1 group by id having count(*) > 1 )
The query contains a correlated subquery in WHERE clause:
SELECT COUNT(*) FROM t1 WHERE t1.id = t.id
It is called correlated because it is related to the main query via t.id. So, this subquery counts the number of records having an id value that is equal to the current id value of the record returned by the main query.
Thus, predicate
(SELECT COUNT(*) FROM t1 WHERE t1.id = t.id) = 2
evaluates to true for any row with an id value that occurs twice in the table.
SELECT * FROM t1 AS t
WHERE 2 = (SELECT COUNT(*) FROM t1 WHERE t1.id = t.id);
This query goes through each record in t1 and then in the subquery looks into t1 again to see if in this case id is found 2 times (and only 2 times). You can do the same for any other column in t1 (or any table for that matter).
When you would like to see all values that are multiple times in the table, change WHERE 2 = by WHERE 1 <. This will also give you the values that are 3 times, 4 times, etc. in the table.
{
SELECT id,count( * )
FROM
MyTable
group by id
having count( * )>1
}
with this code, you can see the rows which repet more than one,
and you can change this query by yourself
How about using GROUP BY and HAVING:
SELECT id, count(1) as Total FROM MyTable AS t1
GROUP BY t1.id
HAVING Total = 2
I table data is like this:
id car_id create_time remark
6c3befd0201a4691 4539196f55b54523986535539ed7beef 2017-07-1 16:42:49 firstcar
769d85b323bb4a1c 4539196f55b54523986535539ed7beef 2017-07-18 16:42:49 secondcar
984660c4189e499 575d90e340d14cf1bef4349b7bb5de9a 2017-07-3 16:42:49 firstjeep
I want to get the newest data. It means if there have two same car_id, I want to get only one according the newest time. How to write?
I try to write this, but I find it may wrong. If the other record may have the same create_time? How to fix that?
SELECT * FROM t_decorate_car
WHERE create_time IN
(SELECT tmptime FROM
(SELECT MAX(create_time),tmptime,car_id
FROM decorate
GROUP BY car_id
) tmp
)
One canonical way to handle this is to join your table to a subquery which finds the latest record for each car_id. This subquery serves as a filter to remove the older records you don't want to see.
SELECT t1.*
FROM t_decorate_car t1
INNER JOIN
(
SELECT car_id, MAX(create_time) AS max_create_time
FROM t_decorate_car
GROUP BY car_id
) t2
ON t1.car_id = t2.car_id AND
t1.create_time = t2.max_create_time
By the way, if you want to continue down your current road, you can also solve this using a correlated subquery:
SELECT t1.*
FROM t_decorate_car t1
WHERE t1.create_time = (SELECT MAX(t2.create_time) FROM t_decorate_car t2
WHERE t2.car_id = t1.car_id)
You were on the right track but you never connected the subquery to the main query using the right WHERE clause.
I am trying to select the COUNT with three tables with one single query (with WHERE conditions).
Here is my code which doesn't work correctly.
SELECT t1.count(id) AS car_model_count,t2.count(id) AS list_item_count,t3.count(id)
FROM `car_model` AS t1
INNER JOIN `list_item` AS t2
INNER JOIN `part_item` AS t3
WHERE t1.user_id=3;
Possible by using Sub-Query OR UNION is possible to get the COUNT from multiple table.
Try this query :
SELECT
(SELECT count(*) FROM `car_model` WHERE user_id=3 ) AS car_model_count,
(SELECT COUNT(*) FROM `list_item` WHERE user_id=3) AS list_item_count,
(SELECT count(*) FROM `part_item` WHERE user_id=3) AS part_item_count;