The used SELECT statements have a different number of columns (REDUX!!)

前端 未结 1 395
时光说笑
时光说笑 2020-11-30 07:53

There\'s another question out there similar to this, but it didn\'t seem to answer my question.

My question is this: why am I getting back this error

相关标签:
1条回答
  • 2020-11-30 08:19

    UNIONs (UNION and UNION ALL) require that all the queries being UNION'd have:

    1. The same number of columns in the SELECT clause
    2. The column data type has to match at each position

    Your query has:

    SELECT f.*, u1.*, u2.* ...
    UNION 
    SELECT fid2 FROM friends
    

    The easiest re-write I have is:

       SELECT f.*, u.*
         FROM FRIENDS AS f
         JOIN USERS AS u ON u.uid = f.fid2
        WHERE f.fid1 = 1 
          AND f.fid2 > 1
    UNION 
       SELECT f.*, u.*
         FROM FRIENDS AS f
         JOIN USERS AS u ON u.uid = f.fid1
        WHERE f.fid2  = 1 
          AND f.fid1 < 1
    ORDER BY RAND()
    LIMIT 6;
    

    You've LEFT JOIN'd to the USERS table twice, but don't appear to be using the information.

    0 讨论(0)
提交回复
热议问题