PHP MySQL 搜索查询

标签 php mysql

我正在编写使用两个表进行 MySQL 搜索的代码。但结果似乎不正确

界面 Interface

“解释器”表结构(保存有关解释器的信息) enter image description here

“terp_attributes”表结构(保存terp可用的属性值id)

EX-  terp x has level 1 and level 2 certification 
Ex2- terp x can translate ASL and Spanish

enter image description here

上表示例数据

enter image description here

我写了一条sql来获取“已认证(ta_attrvalid=1)或1级技能(ta_attrvalid=2)的男性或女性”,并且他还具有“ASL”语言(ta_attrvalid=6)属性,但它似乎不起作用即使我修改了很多。这是我的查询

SELECT * FROM 
interpreters ,terp_attributes 
WHERE (terp_gender='M' OR terp_gender='F') 
AND terp_agencyid=1 AND terp_id=ta_terpid 
AND ( ta_attrvalid=1 OR ta_attrvalid=2) 
AND ( ta_attrvalid=6) 

应该返回“ta_terpid 3”数据作为预测,但我看不到任何结果

谁能帮我解决这个问题吗? 非常感谢

最佳答案

最好将 attributes 表拆分为 interpreters_skillsinterpreters_languagesinterpreters_locations。然而,根据您的实际设计,您将需要多次连接表或使用 EXISTS 子查询,例如:

SELECT * 
FROM interpreters i
WHERE terp_gender IN ('M', 'F')
  AND terp_agencyid=1
  AND EXISTS (
      SELECT *
      FROM terp_attributes a
      WHERE a.ta_terp_id = i.terp_id
        AND ta_attrvalid IN (1, 2)
  )
  AND EXISTS (
      SELECT *
      FROM terp_attributes a
      WHERE a.ta_terp_id = i.terp_id
        AND ta_attrvalid IN (6)
  )

关于PHP MySQL 搜索查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43309664/

相关文章:

java - 将 mysqlconnector 作为类路径时找不到类

mysql - 如何在连接的 InnoDB 表上优化此 COUNT DISTINCT?

mysql - 使用唯一索引限制 MySQL 中每列的行数

mysql - 如何解决sql查询中的语法错误

php - 如何显示以下场景的分组结果?

php - 为什么 PDO 在 wamp 中不起作用?

php - 当文件从本地主机移动到服务器时,登录表单不起作用

php - 显示标准化数据

php - Laravel 用工厂播种但扩展它

php - WordPress 查询结果,其中所有列都与所选 id 的列具有相同的值