简体   繁体   中英

select unique records from table mysql php

I have notification table, which contains notification_id, type, type_id ... I want to select records which have unique type_id.

I am running this query:

SELECT $cols, DISTINCT type_id FROM notification ORDER BY alert_on ASC

There is something wrong here. What is the way to do it?

If I do like this:

SELECT DISTINCT type_id FROM notification

then it will work. but, i need all the columns from that table...

SELECT $cols,type_id FROM notification GROUP BY type_id ORDER BY alert_on ASC
$query = "SELECT DISTINCT ".$cols." FROM notification ORDER BY alert_on ASC";

SELECT DISTINC的减价不是SELECT,即DISTINCT是SELECT DISTINCT。

您可以尝试使用GROUP BY

SELECT $cols, type_id FROM notification GROUP BY type_id ORDER BY alert_on ASC
SELECT DISTINCT column_name,column_name
FROM table_name;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM