Get values for just one column from a broader MySQL query?

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
fgomez
Forum Commoner
Posts: 61
Joined: Mon Sep 26, 2005 11:23 pm
Location: Washington, DC

Get values for just one column from a broader MySQL query?

Post by fgomez »

Hello,

Sorry, I can't come up with a good subject for this question. My PHP-driven website uses a MySQL query that goes something like this:

Code: Select all

SELECT * 
             FROM table
             WHERE type LIKE '%G%'
                AND [a bunch of other stuff]
I have a column in this table called 'area' which contains the key values for another table. What I want to do is throw all values of 'area' that result from the above query (duplicates excluded) into an array. I feel like I should be able to do this without making another call to the database, but I can't figure it out.

Any and all help appreciated.

Thanks!
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Code: Select all

SELECT DISTINCT `columnName` FROM blah blah blah
fgomez
Forum Commoner
Posts: 61
Joined: Mon Sep 26, 2005 11:23 pm
Location: Washington, DC

Post by fgomez »

So then you do suggest a separate call to the database for this?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

You posted the question in Databases thus choosing the likely "solution" with that decision. The only other way is iterating over the result set pulling out the values manually.
fgomez
Forum Commoner
Posts: 61
Joined: Mon Sep 26, 2005 11:23 pm
Location: Washington, DC

Post by fgomez »

Thanks for your help. Am I correct to understand that pulling them out manually is slow/inefficient?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

You would have to do the same for a specific query for the column alone anyways. The difference is consuming less memory in the interim.
Post Reply