简体   繁体   中英

MYSQL The used SELECT statements have a different number of columns

I need to combined 2 tables with the same ids in it but i can't

    SELECT stat.user_id, user.username,
        SUM(stat.vertrag) AS vertrag,
        SUM(stat.zubehoer) AS zubehoer,
        SUM(stat.privat) AS privat,
        SUM(stat.service) AS service,
        SUM(stat.bs_vertrag) AS bus
            FROM statistics stat
        join users user on stat.user_id = user.uid
        WHERE stat.user_id != '0' AND stat.datum LIKE '%$month%'
        GROUP BY stat.user_id
UNION
    SELECT bew.user_id, stat.user_id, user.username,
        SUM(case when bew.log = 'inv_imei' 
                 THEN
                      1 
                 ELSE
                      0 
                 END) AS inv
        FROM user_bewegungen bew
        JOIN users user ON user.uid = bew.user_id
        JOIN statistics stat ON bew.user_id = stat.user_id
        WHERE bew.date LIKE '%$month%'
        GROUP BY bew.user_id
        ORDER BY vertrag DESC

I am dont know how to go now.....

The first select is perfect, and works. now i have add a union because i need to add the row "log". Id's are also in it but i become the error

The used SELECT statements have a different number of columns

Can anyone help?

Each select statement needs to have the same number of columns. Your first one has 7:

SELECT 
   stat.user_id, 
   user.username,
   SUM(stat.vertrag) AS vertrag,
   SUM(stat.zubehoer) AS zubehoer,
   SUM(stat.privat) AS privat,
   SUM(stat.service) AS service,
   SUM(stat.bs_vertrag) AS bus

Your second one has 4:

SELECT 
   bew.user_id, 
   stat.user_id, 
   user.username,
   SUM(case when bew.log = 'inv_imei' THEN 1 ELSE 0 END) AS inv

You can select NULL in the second SELECT for those columns that aren't in the first one.

Make the two operands of the UNION isomorphic. Rename columns and/or create NULL-valued dummy columns as necessary to give them the same shape. FOR EXAMPLE, if we wanted to form the UNION of:

SELECT  a, b, c  
FROM    table1

and:

SELECT  d, e  
FROM    table2

we would logically pair those columns that are of the same types (in this case, let's assume that a and e are of the same type, and that b and d are of the same type) and add an extra NULL-valued column as the third projected attribute of the right-hand SELECT, as follows:

SELECT  b, a, c  
FROM    table1  
UNION  
SELECT  d AS b, e AS a, NULL as c  
FROM    table2

If such an approach seems confusing, you can use table views to simplify the expression. In the preceding example, you could have asserted a view atop table2:

CREATE VIEW t2view( b, a, c )  
AS  
SELECT d, e, NULL  
FROM   table2

and then formulated your UNION as:

SELECT  b, a, c  
FROM    table1  
UNION  
SELECT  *  
FROM    t2view

In UNION , the field numbers should be the same. Use like this:

SELECT stat.user_id, 0, user.username, .... 

SELECT bew.user_id, stat.user_id, user.username, ...

or use something else, what you know, that is a missing field there.

The data types should be the same also.

You are using 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.)

Reference: MySQL Union

Your first select statement has 7 columns and second statement has 4. You should have same number of column and also in same order in both statement. otherwise it shows error or wrong data.

you can see this example

  1. there are two queries both queries have the same number of columns.
  2. column name can be different.
select 'row1' as column1,'row2' as column2
union 
select 'row3' as column11,'row4' as column222 

if you change columns count, it means in first query you are selecting 2 columns and in second query you are using 3 columns then it will through an error (The used SELECT statements have a different number of columns).

select 'row1' as column1,'row2' as column2
union 
select 'row3' as column11,'row4' as column222 ,'rr' as t ;

run both queries you will see differnce.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM