平衡MYSQL select语句

时间:2012-03-22 04:20:23

标签: mysql

我将'vanity_name'和'name'分别插入第一个和第二个SELECT语句。

我得到了一个不匹配的列数错误,我很困惑,因为我在两个select语句中都添加了一个列以保持平衡。

SQL语句:

SELECT id, 
       vanity_name, 
       Date_format(DATE, '%M %e, %Y') AS DATE, 
       TYPE 
FROM   (SELECT resume_id AS id, 
               date_mod  AS DATE, 
               'resume'  AS TYPE 
        FROM   resumes 
        WHERE  user_id = '1' 
        UNION ALL 
        SELECT profile_id, 
               name, 
               date_mod AS DATE, 
               'profile' 
        FROM   profiles 
        WHERE  user_id = '1' 
        ORDER  BY DATE DESC 
        LIMIT 
        5) AS d1 
ORDER  BY DATE DESC 

2 个答案:

答案 0 :(得分:3)

嗯,您在外部选择中有四个列,内部选择中有三个

  

id,vanity_name,date,type

VS

  

id,date,TYPE

答案 1 :(得分:0)

根据括号,您正试图结合:

(SELECT resume_id AS id,date_mod AS date,'resume'AS TYPE FROM恢复WHERE user_id ='1'

SELECT profile_id,name,date_mod AS date,'profile'FROM profiles ... LIMIT 5)

他们显然不匹配。重新定位你的parens。