0

我有一个带有表的样本数据是测试

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

我的查询

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

如何通过结果获得价值是

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

3 回答 3

2
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
于 2013-04-21T15:28:01.913 回答
1
SELECT name, catid FROM test WHERE catid IN (1, 2, 3) GROUP BY catid

我认为您需要比 catid = X OR catid... 更简单的 IN 运算符(并且您需要 OR,而不是 AND)

于 2013-04-21T15:30:26.383 回答
0

根据您想要的输出,这可能是查询:

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

但是根据您问题中提供的信息,很难知道您想要什么。

于 2013-04-21T15:32:30.873 回答