Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/60.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 1222-使用的SELECT语句具有不同的列数_Sql_Mysql_Mysql Error 1222 - Fatal编程技术网

Sql 1222-使用的SELECT语句具有不同的列数

Sql 1222-使用的SELECT语句具有不同的列数,sql,mysql,mysql-error-1222,Sql,Mysql,Mysql Error 1222,为什么我得到的是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

为什么我得到的是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日期隐私pid uid消息

Friends表结构看起来像Fid id buddy\u id invite\u UPDATE状态


pid代表配置文件id。我真的不确定发生了什么。

您使用的是4列关系id、pid、消息和日期与6列关系*=6列墙柱的并集。SQL不允许这样做。

联合中的第一条语句返回四列:

SELECT b.id AS id, 
       b.pid AS pid, 
       b.message AS message, 
       b.date AS date 
  FROM wall_posts AS b 
第二个返回6,因为*扩展为包含来自WALL_Post的所有列:

工会和工会所有经营者要求:

构成联合查询的所有语句中都存在相同数量的列 数据类型必须在每个位置/列匹配 使用:


您在第一个查询中选择了4,在第二个查询中选择了6,因此请将它们匹配起来。

您使用的是MySQL Union

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.)
参考:

第一个select语句有4列,第二个语句有6列,正如您所说的wall_post有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
你看,我是通过发出insert语句而不是union语句得到这个错误的。我的病例差异是

我发布了一个批量插入sql

i、 e.插入表1字段。。。作为选择字段。。。来自表2

tab2有一个on insert触发器;这个触发器基本上拒绝重复

结果是我的触发器出了一个错误。我根据新的输入数据获取记录,并给它们分配了错误数量的变量

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个变量中分配了它们。我的错误是,我完全忘记了在编辑后删除变量。

更改后一个选择,以选择您需要的4列。无论如何,您不应该使用SELECT*:每当有人更改表时,它很容易被破坏。谢谢!这有助于解决我的问题。有助于了解以后的道路也。
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.)
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
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 ;