0

这是我试图加入的两张桌子......

    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”的“通知”表中的行,并且“位置”表中的 contentType 等于“1”。所以对于上面的表数据,我想从返回的“通知”中获取前两行,因为位置表中允许“电子邮件”的 contentType。

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

4

3 回答 3

3

试试这个:

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       ║
╚═════════╩══════════╩═════════╩═════════════╝

看到这个 SQLFiddle

更多数据的 SQLFiddle

于 2013-05-14T07:39:47.903 回答
1

尝试:

select n.*
from notifications n
join position p 
  on n.member = p.member and
     case n.contentType when 'Email' then p.getsEmail 
                        when 'SMS' then p.getsSMS end = 1
where n.member = 'testera'

SQLFiddle在这里

于 2013-05-14T07:38:00.373 回答
0

在这里您可以定义内容类型是电子邮件或短信

$contenttype="Email";
$query="select * from notification,position where position.member='testera' and position.gets".$contenttype."='1' ";
于 2013-05-14T07:39:44.953 回答