1

I have tried using the code below and it says

The used SELECT statements have a different number of columns

because distance column in added is more in 2nd query .How do I resolve something like this or can I get the order right without distance column?I really appreciate any help.THanks in Advance.

SELECT * FROM
(
   SELECT * FROM user u
   INNER JOIN employee e ON (u.empid = e.empid)
   INNER JOIN awards a ON (u.empid = a.empid)
   WHERE u.empid = 123

   UNION

   SELECT * ( 3959 * acos( cos( radians(37) ) * cos( radians( lat ) ) * cos( radians( lng ) - radians(-122) )+ sin( radians(37) ) * sin( radians( lat ) ) ) ) AS distance
   FROM user u
   INNER JOIN employee e ON (u.empid = e.empid)
   INNER JOIN awards a ON (u.empid = a.empid)
   HAVING distance < 25
   ORDER BY distance
) a
ORDER BY timestamp DESC
2
  • The problem is that the second one has distance and the first doesn't, although I would expect the second one to just generate an error. I didn't realize that the comma after * is optional. Jan 19, 2014 at 21:08
  • Thanks for looking into it ,I tried executing the 2nd code and 1st code separately it works fine .btw I had to add , after *.But how can you have UNION for uneven columns like the above case other than the code which you suggested in previous question.Thanks again.
    – jason
    Jan 19, 2014 at 21:11

2 Answers 2

2

It's because your 1st SELECT and 2nd SELECT have varying number of columns, since you introduced Distance in 2nd SELECT.

So try making Distance in 1st query null or 0 as follows.

SELECT * FROM
(
   SELECT *, 0 as Distance
   FROM user u
   INNER JOIN employee e ON (u.empid = e.empid)
   INNER JOIN awards a ON (u.empid = a.empid)
   WHERE u.empid = 123

   UNION

   SELECT *, ( 3959 * acos( cos( radians(37) ) * cos( radians( lat ) ) * cos( radians( lng ) - radians(-122) )+ sin( radians(37) ) * sin( radians( lat ) ) ) ) AS distance
   FROM user u
   INNER JOIN employee e ON (u.empid = e.empid)
   INNER JOIN awards a ON (u.empid = a.empid)
   HAVING distance < 25
   ORDER BY distance
) a
ORDER BY timestamp DESC
0
0

You would fix this by adding distance to the first query:

SELECT * FROM
(
   SELECT *, NULL as distance FROM user u
   INNER JOIN employee e ON (u.empid = e.empid)
   INNER JOIN awards a ON (u.empid = a.empid)
   WHERE u.empid = 123

   UNION

   SELECT *,  ( 3959 * acos( cos( radians(37) ) * cos( radians( lat ) ) * cos( radians( lng ) - radians(-122) )+ sin( radians(37) ) * sin( radians( lat ) ) ) ) AS distance
   FROM user u
   INNER JOIN employee e ON (u.empid = e.empid)
   INNER JOIN awards a ON (u.empid = a.empid)
   HAVING distance < 25
   ORDER BY distance
) a
ORDER BY timestamp DESC;
8
  • I get duplicate column name sno .Sorry.
    – jason
    Jan 19, 2014 at 21:17
  • @BarbC.Goldstein . . . That is a very strong accusation, especially since I made a comment explaining exactly what the problem was long before your post appeared. It is not uncommon for two people to arrive at an answer and to post in close time proximity. Jan 19, 2014 at 21:28
  • @BarbC.Goldstein Sorry Mr.BarbC ,I was in conversation with Mr.Gordon and he posted the code right after that .Thats why I accepted that.Thanks again.Sorry for the trouble.
    – jason
    Jan 19, 2014 at 21:30
  • @GordonLinoff I still get this error : I get duplicate column name SNO how do I fix this ?
    – jason
    Jan 19, 2014 at 21:31
  • @jason . . . Because you don't actually want select *. The same column name may be used in different tables. You can say select u.*, and then list the individual columns from the other tables. Jan 19, 2014 at 21:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.