如何计算另一个表

时间:2014-12-21 16:26:45

标签: php mysql sql

如下图所示,我有两个表,现在我想根据Restaurant列获取cat表中的所有列,以及来自Foods表的具有该Restaurant id的记录数

enter image description here

示例:(从Restaurant表中获取 1,测试,从Foods表中获取 3 )。

$sql = "select * from Restaurant,Foods where cat=6..." ;

更新:

$sql = "r.*,(SELECT COUNT(*) FROM restaurant_foods WHERE restaurant_id = r.id)
    foods_count FROM restaurant r WHERE r.cats LIKE '%,$cat,%' limit $start,$end"

1 个答案:

答案 0 :(得分:1)

那应该做的工作:

SELECT r.*,
       (SELECT Count(*)
        FROM   Foods
        WHERE  restaurnat_id = r.id) foods_count
FROM   Restaurant r
WHERE  r.cat = 6