MySQL WHERE IN查询

时间:2013-01-11 05:55:58

标签: mysql sql

有供稿和喜欢的表格,首先我从供稿​​表中查询数据,然后尝试获取喜欢的表格来获取供稿。

供稿表:

`feed_id` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
`feed_postid` INT(15) UNSIGNED NOT NULL,
`feed_type` VARCHAR(40) NOT NULL,
`feed_userid` INT(12) UNSIGNED NOT NULL,
`feed_privacy` TINYINT(1) UNSIGNED NOT NULL,
`feed_uids` TEXT NULL,
`feed_title` VARCHAR(180) NULL DEFAULT NULL,
`feed_content` TEXT NOT NULL,
`feed_dateline` DATETIME NOT NULL,

喜欢桌子:

like_id
like_userid
like_type
like_postid

现在我想查询表喜欢这样的表

IN((1, 'status'),(2,'image'),(3, 'status'))
// IN((like_postid,like_type),(like_postid,like_type),(like_postid,like_type))

示例查询:

SELECT * FROM likes WHERE like_postid = 1 AND like_type = 'status' OR like_postid = 2 AND like_type = 'image' OR like_postid = 5 AND like_type = 'status';

现在我想用IN函数重写顶级查询

SELECT * FROM likes WHERE like_postid, like_type IN((1,'status'), (2,'image'), (5, 'status')); --but this is not work, there is any way to do this?

2 个答案:

答案 0 :(得分:3)

你可以试试这个,

SELECT * FROM likes WHERE (like_postid, like_type) IN ((1,'status'),(2,'image'),(5,'status'));

答案 1 :(得分:1)

这是执行前N个分析检查的查询之一

SELECT * FROM喜欢WHERE like_type in(' status',' image'),按like_postid限制2排序 联合所有 SELECT * FROM喜欢WHERE like_type in(' status',' image')by like_postid offset 4 limit 1