子查询的SQL问题

时间:2010-12-31 07:53:54

标签: mysql subquery

运行此查询时出现以下错误:

  

#1241 - 操作数应包含1列

SELECT *,
       Date_format(date_assigned, '%d-%m-%y')                     AS datum,
       YEAR(date_assigned)                                        AS YEAR,
       YEAR(NOW())                                                AS this_year,
       Datediff(NOW(), date_assigned)                             AS age,
       MONTH(NOW())                                               AS this_month,
       Concat(Period_add(Date_format(date_released, "%Y%m"), -6)) AS cancel_date
       ,
       Date_format(NOW(), '%Y%m')                                 AS
       this_date
FROM   reseller_numbers
WHERE  reseller_id = '31'
       AND MONTH(date_assigned) = '12'
       AND ( ( date_released IS NULL
                OR date_released > NOW() )
              OR ( Dayofyear(date_assigned) <= Dayofyear(date_released)
                   AND ( date_released IS NULL
                          OR date_released > NOW() ) ) )
       AND ( date_released IS NULL
              OR date_released > NOW()
                 AND date_released NOT LIKE '%2999%'
                 AND date_released != '2038-01-01 00:00:00' )
       AND ( date_released NOT IN (SELECT *,
                                          Date_format(DATE_ADD(date_released,
                                                      INTERVAL '-6' MONTH),
                                          '%d%m%y')
                                          AS
                                          cancel_date
                                   FROM   reseller_numbers
                                   WHERE  reseller_id = '31'
                                   HAVING DATE_ADD(date_released,
                                          INTERVAL '-6' MONTH)
                                          <
                                          NOW(
                                          )) )
ORDER  BY date_released DESC
LIMIT  0, 30 

此查询有问题吗?当我删除date_released NOT IN中的子查询时,查询执行得很好。

4 个答案:

答案 0 :(得分:2)

子查询必须只返回date_released值的一列才能匹配。摆脱“*,”

答案 1 :(得分:1)

在date_released中,您应该只选择一列

SELECT Date_format(DATE_ADD(date_released,
                                                  INTERVAL '-6' MONTH),
                                      '%d%m%y')
                                      AS
                                      cancel_date
                               FROM   reseller_numbers
                               WHERE  reseller_id = '31'
                               HAVING DATE_ADD(date_released,
                                      INTERVAL '-6' MONTH)
                                      <
                                      NOW(
                                      ))

答案 2 :(得分:1)

从子查询中删除*

SELECT *,
       Date_format(date_assigned, '%d-%m-%y')                     AS datum,
       YEAR(date_assigned)                                        AS YEAR,
       YEAR(NOW())                                                AS this_year,
       Datediff(NOW(), date_assigned)                             AS age,
       MONTH(NOW())                                               AS this_month,
       Concat(Period_add(Date_format(date_released, "%Y%m"), -6)) AS cancel_date
       ,
       Date_format(NOW(), '%Y%m')                                 AS
       this_date
FROM   reseller_numbers
WHERE  reseller_id = '31'
       AND MONTH(date_assigned) = '12'
       AND ( ( date_released IS NULL
                OR date_released > NOW() )
              OR ( Dayofyear(date_assigned) <= Dayofyear(date_released)
                   AND ( date_released IS NULL
                          OR date_released > NOW() ) ) )
       AND ( date_released IS NULL
              OR date_released > NOW()
                 AND date_released NOT LIKE '%2999%'
                 AND date_released != '2038-01-01 00:00:00' )
       AND ( date_released NOT IN (SELECT Date_format(DATE_ADD(date_released,
                                                      INTERVAL '-6' MONTH),
                                          '%d%m%y')
                                          AS
                                          cancel_date
                                   FROM   reseller_numbers
                                   WHERE  reseller_id = '31'
                                   HAVING DATE_ADD(date_released,
                                          INTERVAL '-6' MONTH)
                                          <
                                          NOW(
                                          )) )
ORDER  BY date_released DESC
LIMIT  0, 30 

答案 3 :(得分:0)

子查询必须只选择一个值,可能是DATE_FORMAT值。删除*。