简体   繁体   中英

Get unique value from Mysql db

My database table name "Square" structure is look like this:

ID      Name    URL           Role
1       ab      www.url.com   xy
2       cd      www.url.com   xy 
3       ef      www.url.com   xy 
4       ab      www.url.com   xy
5       cd      www.url.com   xy
6       gh      www.url.com   xy

Now, I want to get unique value from this table. For example. Unique name is : ab, cd, ef, gh with id, url and role.

so my sql query is look like this:

$getpara = $_GET['role'];
$getsquare = mysql_query("SELECT ID, DISTINCT (Name), URL, Role FROM Square WHERE Role = 
'$getpara'");

but getting error message, may be my sql query is wrong. can you guyes help me plz ?

Try with GROUP BY instead of DISTINCT like

$getsquare = mysql_query("SELECT ID, Name, URL, Role 
                          FROM Square 
                          WHERE Role = '$getpara' 
                          GROUP BY Name");

And also try to avoid using mysql_* functions due to they are deprecated .Instead of them use mysqli_* functions or PDO statements

使用GROUP BYDISTINCT对所有行进行操作,

$getsquare = mysql_query("SELECT ID, Name, URL, Role FROM Square WHERE Role = '$getpara' GROUP BY Name");
$getpara = $_GET['role'];
$getsquare = mysql_query("SELECT DISTINCT Name, ID, URL, Role FROM Square WHERE Role = 
'$getpara'");

Try this one without the '()' and the DISTINCT first.

只需删除DISTINCT ,您的查询就可以正常工作-由于您具有唯一的名称,因此不需要DISTINCT CLAUSE。

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