mysql从一个表中选择all并检查另一个表的ID和状态

时间:2013-12-18 08:40:13

标签: mysql sql

目前我在下面打印了简单的mysql SELECT查询。

  "SELECT * FROM pictures WHERE views > $views_min ORDER BY $rating_or_something_other DESC LIMIT $index , $pics_per_page";

我想找到所有未暂停的照片,但问题是暂停状态位于另一张桌子上:

picturespictures.gallery_id列,我想检查galleries.id表格中的id,我的最后一点是gallery.hold状态。根据这些数据,我希望仅得到所有未暂停的pictures.*(按图库)。

我正在尝试这样的事情,但似乎需要使用JOINS

  "SELECT p.*, g.id, g.hold FROM pictures AS p, galleries AS g
    WHERE p.views > $views_min AND p.gallery_id = g.id AND g.hold <> 1 ORDER BY p.rating(?) DESC LIMIT $index, $pics_per_page;

1 个答案:

答案 0 :(得分:0)

为什么要阻止使用连接?就这么简单:

SELECT p.*, g.id, g.hol FROM pictures AS p 
 INNER JOIN galleries as g ON g.id = p.gallery_id
WHERE p.views > $views AND g.hold <> 1
ORDER BY p.rating(?) DESC 
LIMIT $index, $pics_per_page;