1

Example tables:

Table_1
 - value_a
 - value_b

Table_2
 - value_c
 - value_d
 - value_e

Trying to do this:

SELECT value_a, value_b UNION ALL SELECT value_c, value_d, value_e

Results in an error with non-matching column count.

Do I need a secondary query to get the other columns that I want, or if say using a JOIN clause will that reduce my query count to just one?

I was looking at this stackoverflow post.

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

Community
  • 1
  • 1

2 Answers2

1

Just add a third column to your first query like

SELECT
 value_a,
 value_b,
 null AS c
FROM yourTable

UNION ALL

....
S3S
  • 24,809
  • 5
  • 26
  • 45
  • That does seem like a simple solution, any drawbacks to this? I currently don't understand how to use JOINs but it seems to be relevant to what I'm trying to do. Still what you suggested sounds like the simplest thing to do. – Jacob David C. Cunningham Dec 15 '16 at 23:19
  • I was also curious if you when you're pulling the data is it easier on the mysql part if you grab data in order according to the table? – Jacob David C. Cunningham Dec 15 '16 at 23:20
  • UNION and JOIN is completely different. one is vertical and one is horizontal. Ordering the data will usually take longer since the data is stored unsorted @JacobDavidCunningham – S3S Dec 15 '16 at 23:24
  • @JacobDavidCunningham there is no way to know which you need. It depends on what you are trying to achieve but these are two of the most basic things to learn in SQL and are definitely paramount. Especially joins. – S3S Dec 15 '16 at 23:25
  • By ordering I meant the order in which I list them, so there's no correlation between top-down (in the db table) versus left-right when listing in select? Yeah no doubt I have to learn JOIN/UNION, the vertical/horizontal is a good tip. – Jacob David C. Cunningham Dec 15 '16 at 23:37
  • No correlation at all. In fact after you write a SELECT or any other statement the optimizer decides the most efficient route to accomplish what you have written so it is t executed in order as you write it always. It's not like python or scripting languages in that sense. @JacobDavidCunningham – S3S Dec 15 '16 at 23:40
  • thanks a lot for your help and the extra information. – Jacob David C. Cunningham Dec 15 '16 at 23:41
  • This is probably not cool, but I did have one last question. If I have a third table and in this table, has a field that matches data from one of the table's column, can I also include that in this single query or is that too much/wouldn't work as you need a comparison operator though there is "WHERE IS" I think I can do it, not sure. – Jacob David C. Cunningham Dec 15 '16 at 23:46
  • It is too much as it should be a new question but questions like these usually get closed since there's a lot of info out there on SQL. What you are talking about is almost certainly a JOIN instead of a UNION but appending, which is what UNION does, wouldn't have a big impact. Obviously the more you do the more taxing it is but this is t anything complicated. @JacobDavidCunningham – S3S Dec 15 '16 at 23:54
  • 1
    Alright thanks, time to go read up, I know people hate on W3Schools but their simple examples are pretty easy to pick up versus the actual manual of MYSQL. just my opinion anyway. – Jacob David C. Cunningham Dec 16 '16 at 00:09
1

wouldn't something like this make sense?

SELECT value_a, value_b, null UNION ALL SELECT value_c, value_d, value_e

This will result will be:

value_a, value_b, null
value_c, value_d, value_e
iceraj
  • 359
  • 1
  • 5