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

接口
php - PHP MySQL搜索查询-LMLPHP

“解释器”表结构(包含有关解释器的信息)php - PHP MySQL搜索查询-LMLPHP

“ terp_attributes”表结构(该属性包含可用于terps的属性值id)

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


php - PHP MySQL搜索查询-LMLPHP

上表中的样本数据

php - PHP MySQL搜索查询-LMLPHP

我写了一条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 - PHP MySQL搜索查询,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43309664/

10-14 14:42
查看更多