簡體   English   中英

#1222 - 使用的 SELECT 語句具有不同的列數

[英]#1222 - The used SELECT statements have a different number of columns

為什么我得到 #1222 - 使用的 SELECT 語句具有不同的列數? 我正在嘗試加載來自該用戶朋友和他自己的牆貼。

SELECT u.id AS pid, b2.id AS id, b2.message AS message, b2.date AS date FROM 
(
    (
        SELECT b.id AS id, b.pid AS pid, b.message AS message, b.date AS date FROM 
        wall_posts AS b 
        JOIN Friends AS f ON f.id = b.pid 
        WHERE f.buddy_id = '1' AND f.status = 'b'
        ORDER BY date DESC
        LIMIT 0, 10
    )
    UNION
    (
        SELECT * FROM
        wall_posts
        WHERE pid = '1'
        ORDER BY date DESC
        LIMIT 0, 10
    )
    ORDER BY date DESC
    LIMIT 0, 10
) AS b2 
JOIN Users AS u
ON b2.pid = u.id
WHERE u.banned='0' AND u.email_activated='1'
ORDER BY date DESC
LIMIT 0, 10

wall_posts 表結構看起來像id date privacy pid uid message

Friends 表結構看起來像Fid id buddy_id invite_up_date status

pid 代表配置文件 ID。 我不太確定發生了什么。

UNION 中的第一條語句返回四列:

SELECT b.id AS id, 
       b.pid AS pid, 
       b.message AS message, 
       b.date AS date 
  FROM wall_posts AS b 

第二個返回6 ,因為 * 擴展為包括來自WALL_POSTS所有列:

SELECT b.id, 
       b.date, 
       b.privacy,
       b.pid. 
       b.uid message
  FROM wall_posts AS b 

UNIONUNION ALL運算符要求:

  1. 構成 UNION 查詢的所有語句中存在相同數量的列
  2. 數據類型必須在每個位置/列匹配

用:

FROM ((SELECT b.id AS id, 
             b.pid AS pid, 
             b.message AS message, 
             b.date AS date 
        FROM wall_posts AS b 
        JOIN Friends AS f ON f.id = b.pid 
       WHERE f.buddy_id = '1' AND f.status = 'b'
    ORDER BY date DESC
       LIMIT 0, 10)
      UNION
      (SELECT id,
              pid,
              message,
              date
         FROM wall_posts
        WHERE pid = '1'
     ORDER BY date DESC
        LIMIT 0, 10))

你走的是UNION 4列關系(的idpidmessagedate )有6列關系( * =的6列wall_posts )。 SQL 不允許您這樣做。

(
        SELECT b.id AS id, b.pid AS pid, b.message AS message, b.date AS date FROM 
        wall_posts AS b 
        JOIN Friends AS f ON f.id = b.pid 
        WHERE f.buddy_id = '1' AND f.status = 'b'
        ORDER BY date DESC
        LIMIT 0, 10
    )
    UNION
    (
        SELECT id, pid  , message , date  
        FROM
        wall_posts
        WHERE pid = '1'
        ORDER BY date DESC
        LIMIT 0, 10
    )

您在第一個查詢中選擇了 4 個,在第二個中選擇了 6 個,因此將它們匹配起來。

除了@omg-ponies 給出的答案; 我只想補充一點,這個錯誤也發生在變量賦值中。 就我而言,我使用了一個插入件; 與該插入相關的是一個觸發器。 我錯誤地將不同數量的字段分配給不同數量的變量。 下面是我的案例詳情。

INSERT INTO tab1 (event, eventTypeID, fromDate, toDate, remarks)
    -> SELECT event, eventTypeID, 
    -> fromDate, toDate, remarks FROM rrp group by trainingCode;
ERROR 1222 (21000): The used SELECT statements have a different number of columns

所以你看到我通過發出一個插入語句而不是聯合語句來得到這個錯誤。 我的案例差異是

  1. 我發出了批量插入sql

    即插入 tab1 (field, ...) 作為選擇字段,... from tab2

  2. tab2 有一個插入觸發器; 這個觸發器基本上拒絕重復

事實證明,我在觸發器中出錯了。 我根據新的輸入數據獲取記錄,並將它們分配到錯誤數量的變量中。

DELIMITER @@
DROP TRIGGER trgInsertTrigger @@
CREATE TRIGGER trgInsertTrigger
BEFORE INSERT ON training
FOR EACH ROW
BEGIN
SET @recs = 0;
SET @trgID = 0;
SET @trgDescID = 0;
SET @trgDesc = '';
SET @district = '';
SET @msg = '';

SELECT COUNT(*), t.trainingID, td.trgDescID, td.trgDescName, t.trgDistrictID
    INTO @recs, @trgID, @trgDescID, @proj, @trgDesc, @district
    from training as t
    left join trainingDistrict as tdist on t.trainingID = tdist.trainingID
    left join trgDesc as td on t.trgDescID = td.trgDescID
    WHERE
    t.trgDescID = NEW.trgDescID
    AND t.venue = NEW.venue
    AND t.fromDate = NEW.fromDate 
    AND t.toDate = NEW.toDate 
    AND t.gender = NEW.gender
    AND t.totalParticipants = NEW.totalParticipants
    AND t.districtIDs = NEW.districtIDs;

IF @recs > 0 THEN
    SET @msg = CONCAT('Error: Duplicate Training: previous ID ', CAST(@trgID AS CHAR CHARACTER SET utf8) COLLATE utf8_bin);
    SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = @msg;
END IF;
END @@ 

DELIMITER ; 

如您所見,我正在獲取 5 個字段,但將它們分配到 6 個變量中。 (我的錯完全是我在編輯后忘記刪除變量。

您正在使用 MySQL 聯盟。

UNION is used to combine the result from multiple SELECT statements into a single result set.

The column names from the first SELECT statement are used as the column names for the results returned. Selected columns listed in corresponding positions of each SELECT statement should have the same data type. (For example, the first column selected by the first statement should have the same type as the first column selected by the other statements.)

參考: MySQL 聯盟

您的第一個 select 語句有 4 列,第二個語句有 6 列,正如您所說的 wall_post 有 6 列。 您應該在兩個語句中具有相同的列數和相同的順序。 否則顯示錯誤或錯誤數據。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM