Hai Truong IT
Hai Truong IT

Reputation: 4187

How to get one value from other categories in php mysql?

I have a sample data with table is test

name | catid | date
------------
abc  |   1
def  |   2
ghi  |   1
jkl  |   2
mno  |   1
pqr  |   3

And my query

SELECT * FROM test WHERE catid = 1 AND catid = 2 AND catid = 3 ORDER BY date DESC

How to get value with result is

    name | catid
    ------------
    abc  |   1
    def  |   2
    pqr  |   3

Upvotes: 0

Views: 79

Answers (3)

Dan Bracuk
Dan Bracuk

Reputation: 20804

Based on your desired output, this might be the query:

select catid, min(name)
from yourtable
where catid between 1 and 3
group by catid

But it's hard to know what you want based on the info provided in your question.

Upvotes: 0

Ivan Yonkov
Ivan Yonkov

Reputation: 7034

SELECT name, catid FROM test WHERE catid IN (1, 2, 3) GROUP BY catid

I think you need the IN operator which is simplier than catid = X OR catid... (and you need OR, not AND)

Upvotes: 1

John Woo
John Woo

Reputation: 263723

SELECT  a.*
FROM    TableName a
        INNER JOIN
        (
            SELECT  catid, MAX(DATE) max_date
            FROM    tableName
            GROUP   BY catID
        ) b ON  a.catID = b.catID AND
                a.date = b.max_date

Upvotes: 2

Related Questions