MySQL的。链接两个表,table1是WORKERS,table2是FIRM。 表1约有50,000条记录,表2约有15,000条记录。使用链接表的内部联接。不适用于未知输入电子邮件。 我来自公司或员工通过电子邮件提供。我通过电子邮件的价值搜索公司及其条件...... 当知道emailW或emailF(变量" $ W_EMAIL")时,查找requestX的值 (来自FIRM或工人的电子邮件。对于某些输入值 - 程序没有响应..)
TABLE_1
id | emailW
------ | ------
100 | "bbbb@bb.com"
100 | "ccc@bb.com"
100 | "bbbb@bb.com"
TABLE_2
id | requestF | emailF
------ | ------------- | ------
100 | "service xx" | "aaa@bb.com"
200 | "service xx" | "bbb1@bb.com"
300 | "service zz" | "bbb2@bb.com"
如果找到bbbb@bb.com ...没问题(工人中存在电子邮件)
找到aaa@bb.com ...没问题(电子邮件存在于FIRM中)
如果找到xxx@yyy.info ...没有回复......没有错误......"白屏"! (如果电子邮件不存在!)
我的代码:
SELECT table1.id,
table1.emailW,
table2.id,
table2.requestF,
table2.emailF
FROM table1
INNER JOIN table2 ON table1.id = table2.id
WHERE table1.emailW='$W_EMAIL'
OR table2.emailF='$W_EMAIL'
答案 0 :(得分:0)
select case when t.obs > 0 then 'Ok' else 'Not Ok' end as 'OKorNotOk'
from
(
select s.*,count(*) Obs
from
(
SELECT table_1.id,
table_1.emailW,
table_2.id t2id,
table_2.requestF,
table_2.emailF
FROM table_1
left join table_2 ON table_1.id = table_2.id
union
SELECT table_1.id,
table_1.emailW,
table_2.id ,
table_2.requestF,
table_2.emailF
FROM table_1
right join table_2 ON table_1.id = table_2.id
) s
where s.emailw = 'aaa@bb.com' or s.emailf = 'aaa@bb.com'
) t
结果
+-----------+
| OKorNotOk |
+-----------+
| Ok |
+-----------+
1 row in set (0.00 sec)
或者
select t.emailtofind,
s.id,s.requestf,
case when s.srce = 't1' then 'y' else '' end as 'ok found in t1',
case when s.srce = 't2' then 'y' else '' end as 'ok found in t2',
case when s.email is null then 'Not Found' else 'Ok' end as Message
from
(
select 't1' as srce,t1.ID,'' as requestf , t1.emailw email from table_1 t1
union
select 't2',t2.ID,t2.requestf , t2.emailf from table_2 t2
) s
right join (select cast('aaa@bb.com' as char(20)) as emailtofind) t on t.emailtofind = cast(s.email as char(20))
结果
+-------------+------+------------+----------------+----------------+---------+
| emailtofind | id | requestf | ok found in t1 | ok found in t2 | Message |
+-------------+------+------------+----------------+----------------+---------+
| aaa@bb.com | 100 | service xx | | y | Ok |
+-------------+------+------------+----------------+----------------+---------+
1 row in set (0.00 sec)