MySQL 许多“和”;声明

发布于 2024-12-14 06:44:05 字数 394 浏览 1 评论 0原文

我有这样的语句,这是错误的方法,但是我如何让该语句发挥作用(该语句的结果需要包含其中的所有 AND )

SELECT a.username, a.first_name, a.last_name
     , b.tx_time, b.account_id
     , a.id 
     , b.table_id, b.tx_type, b.amount 
FROM punter a
   , account_transaction b 
WHERE b.tx_time >= '2011-07-01' 
  AND b.tx_time < '2011-09-30' 
  AND b.account_id = a.id 
  AND b.tx_type = 4 
  AND b.tx_type = 14

I have this statement which is the wrong way to do it but how would I get the statement to work (the result of the statement needs to hold all the AND in it)

SELECT a.username, a.first_name, a.last_name
     , b.tx_time, b.account_id
     , a.id 
     , b.table_id, b.tx_type, b.amount 
FROM punter a
   , account_transaction b 
WHERE b.tx_time >= '2011-07-01' 
  AND b.tx_time < '2011-09-30' 
  AND b.account_id = a.id 
  AND b.tx_type = 4 
  AND b.tx_type = 14

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

猫烠⑼条掵仅有一顆心 2024-12-21 06:44:05

AND b.tx_type = 4 AND b.tx_type = 14 将为您提供空结果集。一列同时只能有一个值。

SELECT a.username, a.first_name, a.last_name, 
       b.tx_time, b.account_id, a.id,
       b.table_id, b.tx_type, b.amount 
FROM punter a 
INNER JOIN account_transaction b ON b.account_id = a.id
WHERE b.tx_time BETWEEN '2011-07-01' AND '2011-09-30' AND b.tx_type IN (4,14) 

AND b.tx_type = 4 AND b.tx_type = 14 will give you empty result set. One column can only have one value at the same time.

SELECT a.username, a.first_name, a.last_name, 
       b.tx_time, b.account_id, a.id,
       b.table_id, b.tx_type, b.amount 
FROM punter a 
INNER JOIN account_transaction b ON b.account_id = a.id
WHERE b.tx_time BETWEEN '2011-07-01' AND '2011-09-30' AND b.tx_type IN (4,14) 
十秒萌定你 2024-12-21 06:44:05

您可以使用显式 JOIN 来简化一点:

SELECT a.username, a.first_name, a.last_name, 
       b.tx_time, b.account_id, a.id,
       b.table_id, b.tx_type, b.amount 
FROM punter a INNER JOIN account_transaction b 
    ON b.account_id = a.id
WHERE b.tx_time BETWEEN '2011-07-01' AND '2011-09-30' 
  AND b.tx_type = 4 
  AND b.tx_type = 14

请注意,我认为使用 b.tx_type = 4 AND b.tx_type = 14 是错误的,因为它始终为 false。如果您需要获取具有 tx_type = 4tx_type = 14 的记录,您可以更改最后的 SQL 行:

AND b.tx_type IN (4,14)

You could use explicit JOIN to simplify a bit:

SELECT a.username, a.first_name, a.last_name, 
       b.tx_time, b.account_id, a.id,
       b.table_id, b.tx_type, b.amount 
FROM punter a INNER JOIN account_transaction b 
    ON b.account_id = a.id
WHERE b.tx_time BETWEEN '2011-07-01' AND '2011-09-30' 
  AND b.tx_type = 4 
  AND b.tx_type = 14

Note that I think it's an error using b.tx_type = 4 AND b.tx_type = 14 because it's always false. If you need to get records having tx_type = 4 or tx_type = 14 you can change last SQL lines with

AND b.tx_type IN (4,14)
好听的两个字的网名 2024-12-21 06:44:05

我只是猜你的意思是 b.tx_type = 4 OR b.tx_type = 14

SELECT a.username, a.first_name, a.last_name, b.tx_time, 
b.account_id, a.id, b.table_id, b.tx_type, b.amount 
FROM punter a, account_transaction b 
WHERE b.tx_time >= '2011-07-01' AND b.tx_time < '2011-09-30' 
AND b.account_id = a.id 
-- AND b.tx_type = 4 AND b.tx_type = 14
AND b.tx_type in (4, 14)

I just guess you meant b.tx_type = 4 OR b.tx_type = 14

SELECT a.username, a.first_name, a.last_name, b.tx_time, 
b.account_id, a.id, b.table_id, b.tx_type, b.amount 
FROM punter a, account_transaction b 
WHERE b.tx_time >= '2011-07-01' AND b.tx_time < '2011-09-30' 
AND b.account_id = a.id 
-- AND b.tx_type = 4 AND b.tx_type = 14
AND b.tx_type in (4, 14)
疯到世界奔溃 2024-12-21 06:44:05

这部分:

AND b.tx_type = 4 
AND b.tx_type = 14

确保您在结果中获得零行。


如果您希望下注者同时进行414类型的交易,则需要JOIN两次进入赌桌b:

SELECT a.username, a.first_name, a.last_name
     , b1.tx_time AS tx_time_1
     , b2.tx_time AS tx_time_2
     , a.id 
     , b1.table_id AS table_id_1, b1.tx_type AS tx_type_1, b1.amount AS amount_1 
     , b2.table_id AS table_id_2, b2.tx_type AS tx_type_2, b2.amount AS amount_2 
FROM punter a
  JOIN account_transaction b1
    ON b1.account_id = a.id 
  JOIN account_transaction b2
    ON b2.account_id = a.id 

WHERE b1.tx_time >= '2011-07-01' 
  AND b1.tx_time < '2011-09-30' 
  AND b1.tx_type = 4

  AND b2.tx_time >= '2011-07-01' 
  AND b2.tx_time < '2011-09-30' 
  AND b2.tx_type = 14

This part:

AND b.tx_type = 4 
AND b.tx_type = 14

ensures that you will get zero rows in the result.


If you want to have punters with transcations of both type 4 and 14, you need to JOIN twice to table b:

SELECT a.username, a.first_name, a.last_name
     , b1.tx_time AS tx_time_1
     , b2.tx_time AS tx_time_2
     , a.id 
     , b1.table_id AS table_id_1, b1.tx_type AS tx_type_1, b1.amount AS amount_1 
     , b2.table_id AS table_id_2, b2.tx_type AS tx_type_2, b2.amount AS amount_2 
FROM punter a
  JOIN account_transaction b1
    ON b1.account_id = a.id 
  JOIN account_transaction b2
    ON b2.account_id = a.id 

WHERE b1.tx_time >= '2011-07-01' 
  AND b1.tx_time < '2011-09-30' 
  AND b1.tx_type = 4

  AND b2.tx_time >= '2011-07-01' 
  AND b2.tx_time < '2011-09-30' 
  AND b2.tx_type = 14
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文