【问题标题】:MySQL: Subquery: Warning Message MeaningMySQL:子查询:警告消息含义
【发布时间】:2016-08-03 18:17:04
【问题描述】:

我写了一个查询来报告将于 2016 年到期的信用卡。它运行,但我确实收到了警告,我想了解更多关于原因的信息。我认为这是因为子查询。

警告:数据值不正确:第 1 行“exp_date”列的“2016%”

我只有一个值满足 2016 年的要求,但出现警告是因为未来可能的值可能满足相同条件吗?

SELECT customer_id as 'Customer'
FROM customers
WHERE credit_card_id = (
        SELECT credit_card_id
        FROM credit_cards
        WHERE exp_date LIKE '2016%'
        LIMIT 1
    );

信用卡值:

INSERT INTO credit_cards VALUES 
(1, '0025184796520000', '2016-08-13', 'Sarah', 'Jones', 3351, '2490 Paseo Verde parkway, suite 150', 'San Diego','CA',92124),
(2, '7896541232548526', '2017-09-21', 'Desmond', 'Lowell', 1204, '3201 Kelsey Street, suite 109', 'San Diego','CA',92174),
(3, '1234567890123456', '2018-02-11', 'Mark', 'Jefferson', 1591, '876 Silverado Street, suite 304', 'Henderson','NV',89162),
(4, '4001330852539605', '2017-01-10', 'Jaime', 'Evans', 8879, '924 Shady Pines Circle, suite 120', 'Summerlin','NV',89074);

【问题讨论】:

  • 他们不是大声笑。这是为作业制作的虚假数据。如果其中任何一个是正确的,我将放弃一切并尝试赢得彩票。
  • 哦,非常感谢。很高兴知道...而且看起来好多了。

标签: mysql sql subquery warnings


【解决方案1】:

问题在于数据类型 DATE TEXT 和来自 '2016%' toDATE` 的隐式转换必须失败。

你可以使用(不是SARGable):

SELECT customer_id as 'Customer'
FROM customers
WHERE credit_card_id = (
        SELECT credit_card_id
        FROM credit_cards
        WHERE YEAR(exp_date) =  2016
    );

第二个改进是使用JOIN:

SELECT DISTINCT c.customer_id AS `Customer`
FROM customers c
JOIN credit_cards cc
  ON cc.credit_card_id = c.credit_card_id
WHERE YEAR(cc.exp_date) = 2016;

最后,你可以使用SARGable

WHERE cc.exp_date >= '2016-01-01' AND cc.exp_date < '2017-01-01'

【讨论】:

  • 尽管我更喜欢使用连接,但我必须在这个被遗弃的任务中的某个地方使用子查询,这就是我的逃避。但我认为我将不得不选择其他更合乎逻辑的东西......但感谢您的解决方案和 YEAR(exp_date) =... 部分,这对未来的场合非常有帮助。
猜你喜欢
  • 2013-06-12
  • 2021-01-19
  • 1970-01-01
  • 2012-12-27
  • 2016-03-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多