gpt4 book ai didi

php - 复杂的 MySQL 加入动态 where

转载 作者:太空宇宙 更新时间:2023-11-03 11:00:13 28 4
gpt4 key购买 nike

这是我要加入的两个表...

    name: notifications
-------------------------------------------
content | position | member | contentType
-------------------------------------------
test a | manager | testera | Email
test b | manager | testera | Email
test c | manager | testera | SMS
-------------------------------------------

name: position
-----------------------------------------------
position | member | getsEmail | getsSMS
-----------------------------------------------
manager | testera | 1 | 0
employee | testerb | 0 | 1
-----------------------------------------------

我想返回成员为“testera”的“通知”表中的行,并且“位置”表中的内容类型等于“1”。所以对于上面的表数据,我想从返回的“notifications”中获取前两行,因为位置表中允许使用“Email”的contentType。

这可能与我的表结构有关吗?

最佳答案

试试这个:

SELECT n.* 
FROM notifications n
JOIN position p
ON n.member = p.member
WHERE (n.contentType = 'Email' AND p.getsEmail = 1)
OR (n.contentType = 'SMS' AND p.getsSMS = 1)

结果:

╔═════════╦══════════╦═════════╦═════════════╗
║ CONTENT ║ POSITION ║ MEMBER ║ CONTENTTYPE ║
╠═════════╬══════════╬═════════╬═════════════╣
║ test a ║ manager ║ testera ║ Email ║
║ test b ║ manager ║ testera ║ Email ║
╚═════════╩══════════╩═════════╩═════════════╝

See this SQLFiddle

SQLFiddle with more data

关于php - 复杂的 MySQL 加入动态 where,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16537356/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com