如何构造查询

时间:2010-08-03 18:35:22

标签: php mysql

我对构建查询感到困惑。我正在餐馆网站上实现搜索功能。用户首先选择他正在搜索餐馆的区域,然后从复选框中选择中文,日文,泰文等食物类型。

在选择所有这些之后,将显示在所选区域中提供所选食物的餐馆。我成功地从yser获得了pincodes和食物类型。

我有两个包含以下字段的表格,

带有字段的restaurant_dp表

id  -ID of the restaurant
pcode -pincode of the area

带有字段的restaurant_iw表

id  - id of the restaurant
menu  - menu the restaurant provides (eg., Chinese, thai etc.,)

我的困惑是如何使用以下条件从两个表中获取记录:

  • pcode = Userselectedpincode
  • menu = userselected menu

Plz帮助。任何帮助将不胜感激

5 个答案:

答案 0 :(得分:4)

select dp.id
from restaurant_dp dp
inner join restaurant_iw iw on dp.id = iw.id
where dp.pcode = Userselectedpincode 
    and iw.menu = userselectedmenu

答案 1 :(得分:1)

SELECT * 
FROM restaurant_dp AS dp
LEFT JOIN restaurant_iw AS iw
ON dp.IDrestaurant= iw.IDrestaurant
WHERE dp.pcode = Userselectedpincode 
AND iw.menu = userselectedmenu

答案 2 :(得分:1)

SELECT DISTINCT dp.id FROM restaurant_dp dp INNER JOIN restaurant_iw iw ON dp.id = iw.id WHERE dp.pcode = $pcode AND iw.menu = $menu

答案 3 :(得分:0)

select * from restaurant_dp dp join restaurant_iw iw on dp.pcode = iw.pcode where dp.pcode = $userselectedpincode and iw.menu = $userselectedmenu;

然后你必须确保正确填充查询中使用的两个变量。

答案 4 :(得分:0)

你真的应该将这些合并到一个表中,所有字段都与同一行中的某个餐馆有关。如果您这样做,查询将变为SELECT * FROM restaurants WHERE pcode = 'code' AND menu = 'menu';。您当前的设置使事情变得更加困难,需要加入:

SELECT * FROM restaurant_iw AS iw
LEFT JOIN restaurant_dp AS dp ON iw.id = dp.id
WHERE dp.pcode = 'code' AND iw.menu = 'menu';