【问题标题】:mysql inner join table1 and table 2mysql内部连接表1和表2
【发布时间】:2016-11-02 12:28:30
【问题描述】:

Mysql.链接两个表,table1 是 WORKERS,table2 是 FIRM。 Table1 大约 50,000 条记录,Table2 大约 15,000 条记录。对链接表使用内部联接。不适用于未知输入电子邮件。 我来自公司或员工的电子邮件报价。我通过电子邮件的价值搜索公司及其条件...... 当知道 emailW 或 emailF(变量 "$W_EMAIL" )时,查找 requestX 的值 (来自 FIRM 或 WORKERS 的电子邮件。对于某些输入值 - 程序没有响应..)

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'

【问题讨论】:

  • <blink>?这个问题绝对是一团糟,完全不可读。我们很乐意提供帮助,但您必须实际描述问题。随意敲击键盘与提问不同。
  • @David,你非常温柔。
  • 我的英文不好。程序代码是可读的,它适用于表 1 和 2 中的现有电子邮件。不起作用 - 表 1,2 内的电子邮件没有响应 - 只有“白屏”......这是问题。表 2 中的电子邮件是正确的 aaa@bb.com ... 对于 id = 100
  • @AgroBiz:“白屏”并不是对问题的真正有用描述。您将不得不更具体地确定正在发生的事情以及失败的位置/方式。没有理由简单地执行 SQL 查询会产生“白屏”。 (什么屏幕?您甚至使用什么来执行查询?)查询将返回零个或多个结果的记录集,或者会产生错误消息。

标签: php mysql join response


【解决方案1】:
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)

【讨论】:

  • 不幸的是,这对我来说非常复杂。我不明白这一点。 “我不确定你的意思是什么”
  • 对我来说很重要——所有 3 个变体的结果:bbbb@bb.com ... aaa@bb.com ... xxx@yyy.info = print: find or no find跨度>
  • 您希望在同一个查询中搜索 3 封电子邮件? $W_email 来自哪里?
  • 你没有回答我的问题
  • 两个查询都会这样做。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多