2016-11-02 87 views
-2

Mysql。鏈接兩張表,table1是WORKERS,table2是FIRM。表1約50000條記錄和表2約15000條記錄。使用內部連接進行鏈接表。不適用於未知的輸入電子郵件。 我來自公司或員工提供的電子郵件。我通過電子郵件的價值搜索公司及其條件... 當知道emailW或emailF(變量「$ W_EMAIL」) (來自公司或工作人員的電子郵件帖子)時,查找requestX的值。值 - 程序沒有響應。)mysql內連接表1和表2

TABLE_1

id  | emailW 
------ | ------ 

100 | "[email protected]" 
100 | "[email protected]" 
100 | "[email protected]" 

TABLE_2

id  | requestF  | emailF 
------ | ------------- | ------ 
100 | "service xx" | "[email protected]" 
200 | "service xx" | "[email protected]" 
300 | "service zz" | "[email protected]" 

如果發現BBBB @ BB .com ...沒問題(電子郵件存在於WORKERS中)
find [email protected] ...沒問題(電子郵件存在於FIRM中)
if find [email protected] ...沒有迴應......沒有錯誤......「白色屏幕」! (如果不存在的郵件!)

我的代碼:

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' 
+1

''?這個問題絕對是一團糟,完全不可讀。我們很樂意提供幫助,但您必須真實地描述問題。在鍵盤上隨意敲打與提問不一樣。 – David

+0

@大衛,你非常溫柔。 – FDavidov

+0

我的英語不好。程序代碼是可讀的,它對於表1和表2中的現有電子郵件是可讀的。不工作 - 對錶1,2中的電子郵件沒有響應 - 只有「白色屏幕」..這是問題。表2中的電子郵件地址是正確的[email protected] ... for id = 100 –

回答

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 = '[email protected]' or s.emailf = '[email protected]' 
) 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('[email protected]' 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 | 
+-------------+------+------------+----------------+----------------+---------+ 
| [email protected] | 100 | service xx |    | y    | Ok  | 
+-------------+------+------------+----------------+----------------+---------+ 
1 row in set (0.00 sec) 
+0

不幸的是,這對我來說非常複雜。我不明白這一點。 「我不確定你的意思是什麼,」 –

+0

對我來說很重要 - 所有3種變種的結果:[email protected] ... [email protected] ... [email protected] = print:找不到或找不到 –

+0

您希望在同一查詢中搜索3封電子郵件? $ W_email從哪裏來? –