【发布时间】:2011-04-08 04:44:03
【问题描述】:
假设我三个表: *食谱 - 包含食谱 *成分 - 项目清单 * 膳食 - 膳食列表
我有一个生成如下选择的表单:
Choose what ingredients you have:
- apples
- bananas
- cherries
Choose the meal this is for:
- breakfast
- lunch
- dinner
我希望用户能够从上述任何一个或一个都不选择,即他们可以选择苹果或樱桃或(香蕉 && 午餐)
当我查询 MySQL 时,我的查询大致是
select recipes.* from recipes
或
select recipes.* from recipes, ingredients
where recipes.id= ingredients.id and ingredients.list in ('apple');
或
select recipes.*
from recipes, ingredients, meal
where recipes.id= ingredients.id
and ingredients.list
and ingredients.id = meals.id
and ingredients.list ('apple')
and meals.list in ('lunch');
如果这个数组存在(即 is_array(ingredients) 添加到查询表(ingredients)并在最后添加 (".ingredients.list in ('apple' ))...
无需编写所有可能的组合或可能的输入(即用户从成分列表中选择,或从成分和膳食列表中选择,或从无列表中选择)?
【问题讨论】:
-
+1 表示格式精美的问题,尤其是对于新用户!您是否本质上想要“搜索条件” - 提取“所有涉及苹果的食谱”和“所有涉及香蕉的午餐食谱”等?
-
你考虑过使用 ORM 吗?它可以让您轻松构建动态查询。
-
不是很好。实际上很难阅读很长的一行
标签: php mysql append user-input