简体   繁体   English

在MySQL查询中互相检查两个表

[英]Checking two tables against each other in MySQL query

I feel like I'm writing a word problem, but it's really puzzling me and I really hope someone here can solve it: 我觉得我在写一个单词问题,但这确实让我感到困惑,我真的希望这里有人可以解决这个问题:

I want to select one row from table A. Table A includes the attributes Name and Number. 我想从表A中选择一行。表A包含属性Name和Number。 But before I finish the query, I want to check it against table B. Table B includes Name, Number, and the Username of the user. 但是在完成查询之前,我想对照表B进行检查。表B包含用户的名称,号码和用户名。 Based on the users' input, it inserts rows into table B that include their Username along with a Name and Number. 根据用户的输入,它将在表B中插入行,其中包括他们的用户名以及名称和号码。 Now in my query where I select a row from table A, I want to make sure that there are no rows in table B with matching Name and Number for that particular User. 现在,在查询中,我从表A中选择一行,我想确保表B中没有该特定用户的具有匹配名称和编号的行。

I have tried WHERE (A.Name = B.Name AND A.Number = B.Number AND B.Username != '$username') but I think I was way off base with that. 我已经尝试过WHERE(A.Name = B.Name AND A.Number = B.Number AND B.Username!='$ username'),但是我认为我已经脱离了基础。 Any help would be... amazing. 任何帮助都将……令人惊讶。

SELECT
   A.id
FROM
   A
   LEFT OUTER JOIN B ON
      (A.Name = B.Name AND A.Number = B.Number)
WHERE
   B.Name IS NULL
   AND B.Number IS NULL
   AND B.Username = ?
select a.id
from a
where
a.name=:name
and
not exists(select 1 from b where b.id=a.id and b.name=a.name)
  IF NOT EXISTS  ( SELECT 1 
       FROM tableA a
       INNER JOIN tableB b
       ON a.name = b.name
       AND a.number= b.number
       AND b.UserName = 'user' and b.name = 'name'and b.number = 'number')
  SELECT *
  FROM tableA x
  WHERE x.name = 'name'and x.number = 'number'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM