tags:

views:

46

answers:

3

i want to select from a mysql table

title , url from the table

title can be duplicate so i need distinct title from them'

if i use sachin's code then i find duplicate rows so how i can get the information where title not show again as the result. means no duplicate title get from table in mysql

+5  A: 
SELECT  DISTINCT *
FROM    mytable

Update:

SELECT  b.title, b.url
FROM    (
        SELECT  DISTINCT title
        FROM    blah
        WHERE   cID = 1856
        ) bd
JOIN    blah b
ON      b.id = 
        (
        SELECT  bi.id
        FROM    blah bi
        WHERE   bi.cID = 1856
                AND bi.title = bd.title
        ORDER BY
                cid, title, url, id
        LIMIT 1
        )

Create an index on (cid, title, url, id) for this to work fast.

Quassnoi
A: 
SELECT distinct a.title, a_url FROM blah WHERE a.cID = 1856 
Sachin Shanbhag
@zinka: edited the query, please check
Sachin Shanbhag
`DISTINCT` applies to the whole record, so you query may return duplicates on `title`. You may omit the parenthesis, they are only confusing.
Quassnoi
@sachin please review my question !
zinka
A: 

here is the sollution

SELECT * FROM blah WHERE info.cID = 1856 GROUP BY title

zinka
This will work, though it does not guarantee which `url` will be returned for each `title`.
Quassnoi
Correct me If I am wrong, I think this wont work unless you have your "a_url" column in the group by clause too which will again get you duplicate entries
Sachin Shanbhag
@Sachin: http://dev.mysql.com/doc/refman/5.0/en/group-by-hidden-columns.html
Quassnoi
@Quassnoi: Thank you very much. This was a new information to learn for me. Very helpful indeed.
Sachin Shanbhag