MySQL Select multiple columns as one

Posted on 2004-11-22
Last Modified: 2008-01-09
Hi, I have a table with the following fields in:


I want to select them all as one column and loop through them all populating a single listbox with them.

I tried combining them all using syntax like

"SELECT (image1, image2, image3) AS image FROM tblimages"

but this didn't work, it just gave me an error. I also tried:

"SELECT image (image1, image2, image3) FROM tblimages"

but this also gave me an error. Lastly I tried

"SELECT image1 AS image, image AS image, image3 AS image FROM tblimages"

This didn't give me an error but it stil returned 3 seperate columns but all with the same name.

I know this is going to be really simple isn't it! Please help!
Question by:m175400
    LVL 48

    Expert Comment

    what do you mean by one column? You can use concat.

    SELECT  concat(image1, image2, image3) AS image FROM tblimages

    If you want to get those three columns as three seperate rows you will need multiple joins and if -conditions in your query and the query will be come very complicated.
    LVL 1

    Author Comment

    I want all the columns to be returns as if they're just one column, one record after the other. I don't want to actually combine the values into new values, I want all the records from all the columns and to be able to page through all the rows sequentially just as if I'd done

    SELECT image1 FROM tblImages

    Make sense now?

    LVL 48

    Expert Comment

    If you want to do that, The only solution I can think of is having a second table (ctable) only a id-column with three rows where id is 1,2 and 3
    CREATE TABLE ctable (id int);
    insert INTO ctable VALUES (1), (2), (3);

    Then use this select:
    Select if(, image1,if(, image2, image3)) FROM  tblimages a,  ctable b

    Ther migth be a solution to do it with one tbale, but that is very complicated and beyond my knowledge

    LVL 20

    Accepted Solution

    or alternately use the union syntax:

    ( SELECT image1 as image FROM tblimages)
    (SELECT image2 as image FROM tblimages)
    (SELECT image3 as image FROM tblimages)

    (which is the right way of saying: SELECT image1 AS image, image AS image, image3 AS image FROM tblimages)

    problematically this will require a version of MySQL of 4.0+
    LVL 1

    Author Comment

    Many thanks! That seems to work! One weird thing though - I get one extra record with a null value retrieved? Any ideas?
    LVL 1

    Author Comment

    Ah nevermind, sorted it.

    Thanks again guys!

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Introduction In this installment of my SQL tidbits, I will be looking at parsing Extensible Markup Language (XML) directly passed as string parameters to MySQL 5.1.5 or higher. These would be instances where LOAD_FILE (…
    All XML, All the Time; More Fun MySQL Tidbits – Dynamically Generate XML via Stored Procedure in MySQL Extensible Markup Language (XML) and database systems, a marriage we are seeing more and more of.  So the topics of parsing and manipulating XM…
    This video discusses moving either the default database or any database to a new volume.
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    7 Experts available now in Live!

    Get 1:1 Help Now