1

I want to do something similar to the following in MySQL, but every permutation I try gives me errors.

SELECT five,six FROM (SELECT 5 AS five),(SELECT 6 AS six);

I was under the impression that SELECT's output table's, so that I could use SELECT whereever I could insert a table.

Can someone explain the subtleties of the structre of MySQL that prevents this statement from working as well as providing me a solution that does?

I know that it's got to be simple, but I haven't been able to find an answer to this anywhere.

2
  • This works fine in MySQL what version of MySQL are you using?
    – garyj
    Mar 24, 2011 at 22:01
  • 5.0.37, but my employer may have customized things.
    – Rob
    Mar 24, 2011 at 22:04

2 Answers 2

1

Try adding aliases:

SELECT five, six FROM (SELECT 5 AS five) AS a, ( SELECT 6 AS six) AS b

2
  • Every derived table needs an alias. (the "AS a", "AS b" parts in the query) The error message for your original query was: "Every derived table must have its own alias"
    – Ed.C
    Mar 24, 2011 at 22:12
  • Because you can use the selects as a field as well. select (select 5 as five) as five. Without the aliases, MySQL wouldn't be able to tell where the sub-select is coming from.
    – Marc B
    Mar 24, 2011 at 22:24
0

I don't know what you're trying to accomplish but this works

select five,six from (select 5 as five, 6 as six) as t;

It's obvious useless cause it's the same of

select 5 as five, 6 as six

but I hope that it helps you.

1
  • I was just simplifying the example. I actually have 2 massive SELECT statements, so it'd be much more difficult to merge them.
    – Rob
    Mar 24, 2011 at 22:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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