MYSQL - 查询以查看其他表(很难解释...)

时间:2010-03-12 02:36:32

标签: sql mysql select

我有一个查询,其中包含订阅了30天的简报试用版的电子邮件列表..

$thirty = time() - 3024000;

SELECT c.email 
  FROM tbl_clients AS c 
  JOIN tbl_clientoptions AS o ON o.client = c.id 
 WHERE o.option = 'newsletter' 
   AND c.datecreated > $thirty

我想要做的是检查同一个查询,这样如果客户端有tbl_clientoptions.option ='trialoverride',它还会返回超过30天的客户端(即客户机选项表中的一行,其值为“ trialoverride“)

TBL_CLIENTS表:

  • ID
  • 名称
  • 电子邮件
  • dateCreated会

TBL_CLIENTOPTIONS表:

  • ID
  • 客户端
  • 选项

2 个答案:

答案 0 :(得分:0)

怎么样:

SELECT c.email 
   FROM tbl_clients AS c JOIN tbl_clientoptions AS o
   ON o.client = c.id 
   WHERE (o.option = 'newsletter' AND c.datecreated > $thirty) 
     || o.option = 'trialoverride';

答案 1 :(得分:0)

通常只有一个OR条件会执行此操作,如果同一用户用户同时具有这两个条件,则添加DISTINCT,否则将其删除:

SELECT DISTINCT c.email 
FROM tbl_clients c 
 INNER JOIN tbl_clientoptions o ON (o.client = c.id) 
WHERE
 (o.option = 'newsletter' AND c.datecreated < $thirty) OR
 (o.option = 'trial override' AND c.datecreated > $thirty);