是否可以在WHERE(MYSQL)中合并相同的选择查询

时间:2018-02-28 06:34:28

标签: mysql

我的情况:   表AAAA:其他表;       (包括column1:user1_seq,column2:user2_seq,...)   表BBBB:用户表;

  1. 我在POST BODY DATA中只收到了email_info
  2. 我想选择所有数据(user1_seq == email_info || user2_seq == email_info)
  3. 目前我的查询:

    SELECT * FROM AAAA
        WHERE
            user1_seq = (SELECT seq FROM BBBB WHERE user_email = 'test@mail.com')
        OR
            user2_seq = (SELECT seq FROM BBBB WHERE user_email = 'test@mail.com')`
    

    但选择查询重复!! ..怎么解决呢???

1 个答案:

答案 0 :(得分:0)

试试这个(Inner Join) -

select AAAA.*
from AAAA
inner join BBBB on (AAAA.user1_seq = BBBB.seq OR AAAA.user2_seq = BBBB.seq)
where BBBB.user_email = 'test@mail.com'

以上语法适用于MS SQL SERVER。 检查您是否需要Innerleft join