我正在尝试弄清楚如何只选择出现在两个不同表中的数据。这是表结构:
这是 clientInfo
表:
--------------------------------------------------
| Name | Region ID | other info |
--------------------------------------------------
| Bob | 12 | likes cookies |
--------------------------------------------------
| Joe | 20 | scuba diver |
--------------------------------------------------
这是地区
表:
----------------------------------------
| Location | Region ID | Published |
----------------------------------------
| California | 12 | 1 |
----------------------------------------
| Oregon | 4 | 1 |
----------------------------------------
| Washington | 8 | 0 |
----------------------------------------
| Colorado | 20 | 1 |
----------------------------------------
我想使用查询通过下拉菜单进行选择,该菜单仅在状态已发布且客户端属于其中一个状态时才选择该状态。
编辑:在昨天的匆忙中,我意识到我没有把我的问题说清楚。我希望能够将州的下拉菜单填充为搜索参数。因此,根据以上数据,网页的下拉菜单将根据哪些州实际拥有属于他们的客户来填充下拉菜单。
------------------------------ ------------- | Choose State to Search | ▼ | | Search | ------------------------------ ------------- | California | | Colorado | ------------------------------
我该如何表达这个 mysql 查询语句?
最佳答案
我会使用exists
:
select r.*
from regions r
where r.published = 1 and
exists (select 1 from clientinfo ci where r.region_id = ci.region_id);
关于php - 选择出现在 MySQL 中 2 个表上的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23945133/