TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 11-01-2009, 07:02 PM   #1 (permalink)
is cute and cuddly
 
delayedinsanity's Avatar
 
Join Date: Mar 2008
Location: Vegas, Baby
Posts: 963
Thanks: 31
delayedinsanity is on a distinguished road
Default Multiple WHERE clause for each column?

Does anybody know if it's possible in MySQL to attach seperate WHERE clauses to each column being selected in a single query?

I'm running three queries to get data from a single table (so I can't use a join, afaik), which retrieves five columns. One column is the same amongst all three queries, and is how I join them in PHP afterwards. The first query selects two more individual columns, and the second and third one more each.

What I'd like to do is something akin to (and I know this is improper syntax, it's for demonstration of the concept):

php Code:
SELECT column WHERE column = this, column_two WHERE column_two = this FROM table

If it's possible, it would be much simpler than looping through and merging the results after the fact.
delayedinsanity is offline  
Reply With Quote
Old 11-01-2009, 08:19 PM   #2 (permalink)
The Addict
 
Enfernikus's Avatar
 
Join Date: Jun 2008
Posts: 335
Thanks: 2
Enfernikus is on a distinguished road
Default

AND ( Char limit )
__________________
My Blog
Enfernikus is offline  
Reply With Quote
Old 11-01-2009, 09:03 PM   #3 (permalink)
is cute and cuddly
 
delayedinsanity's Avatar
 
Join Date: Mar 2008
Location: Vegas, Baby
Posts: 963
Thanks: 31
delayedinsanity is on a distinguished road
Default

No, no... I'm not that simple. :)

I meant distinct WHERE clauses for each column returned, as opposed to one that restricts the results globally. The first query needs to know a certain column is empty. The next two require that it isn't.

It doesn't seem like it's probable though... I'll just have to stick with using three queries instead of rolling it into one.
delayedinsanity is offline  
Reply With Quote
Old 11-01-2009, 09:21 PM   #4 (permalink)
The Wanderer
 
Join Date: Aug 2009
Posts: 17
Thanks: 0
Rhinos is on a distinguished road
Default

I don't know if I fully understand what you are asking without knowing more details but why can't you just select the five columns and use a UNION for the different WHERE conditions.

Kinda like:

Code:
(SELECT col1, col2, col3, col4, col5
FROM table
WHERE col1 = 'whatever')
UNION
(SELECT col1, col2, col3, col4, col5
FROM table
WHERE col2 = 'whatever')
UNION
(SELECT col1, col2, col3, col4, col5
FROM table
WHERE col3 = 'whatever');
Similar to how you described doing it in PHP however it may have better performance because it is just commissioning one query to mysql instead of the 3 and it may be faster in joining the results together also I don't know.

If I am completely wrong then by all means just ignore me :)
Rhinos is offline  
Reply With Quote
Old 11-01-2009, 10:22 PM   #5 (permalink)
is cute and cuddly
 
delayedinsanity's Avatar
 
Join Date: Mar 2008
Location: Vegas, Baby
Posts: 963
Thanks: 31
delayedinsanity is on a distinguished road
Default

Won't work either, because the data returned in each column differs when you change the WHERE clause, so the UNION creates multiple groups for each timestamp.

If I then went one step further and created a virtual table using unions and grouped the results of that data, it would be severely skewed, as it would add columns that it's selected twice, so I'm not sure that UNION is the answer here.
delayedinsanity is offline  
Reply With Quote
Old 11-06-2009, 04:04 AM   #6 (permalink)
The Contributor
 
Join Date: Jun 2009
Location: Seattle, WA
Posts: 76
Thanks: 1
rguy84 is on a distinguished road
Default

I am not understanding what you are saying at all. Can you provide sample data, and what your result should look like?

Why would you use 3 queries to get data from one table, that seems terribly ineffient. Look into SQL Case (they are like if/else loops)...
__________________
Ryan | Blog | Twitter
Send a message via AIM to rguy84 Send a message via MSN to rguy84 Send a message via Yahoo to rguy84 Send a message via Skype™ to rguy84
rguy84 is offline  
Reply With Quote
Old 11-06-2009, 04:26 AM   #7 (permalink)
is cute and cuddly
 
delayedinsanity's Avatar
 
Join Date: Mar 2008
Location: Vegas, Baby
Posts: 963
Thanks: 31
delayedinsanity is on a distinguished road
Default

Because the various data sets I need are all based on different stipulations.

I need a set of results for WHERE this = 'that' and a second set of results for the exact opposite, WHERE this <> 'that', and of course you cannot combine the two with AND as it would return nothing.

What I meant was on par with doing something like the following (which I know is not possible before anybody tells me):

SELECT this AS first WHERE this = '' this AS second WHERE this <> '' FROM `table`

Therefore I am forced to use

SELECT this AS first FROM `table` WHERE this = ''
SELECT this AS second FROM `table WHERE this <> ''

(these are simplified for the sake of demonstration).
delayedinsanity is offline  
Reply With Quote
Old 11-06-2009, 04:33 AM   #8 (permalink)
The Contributor
 
Join Date: Jun 2009
Location: Seattle, WA
Posts: 76
Thanks: 1
rguy84 is on a distinguished road
Default

yes
Quote:
SELECT this AS first WHERE this = '' this AS second WHERE this <> '' FROM `table`
is not valid SQL...
If you need DIFFERENT data sets displayed, why not make DIFFERENT queries?
__________________
Ryan | Blog | Twitter
Send a message via AIM to rguy84 Send a message via MSN to rguy84 Send a message via Yahoo to rguy84 Send a message via Skype™ to rguy84
rguy84 is offline  
Reply With Quote
Old 11-09-2009, 10:46 PM   #9 (permalink)
is cute and cuddly
 
delayedinsanity's Avatar
 
Join Date: Mar 2008
Location: Vegas, Baby
Posts: 963
Thanks: 31
delayedinsanity is on a distinguished road
Default

I know it's not valid SQL.

me: I'm running three queries to get data from a single table
you: Why would you use 3 queries to get data from one table?
me: Because the various data sets I need are all based on different stipulations.
you: why not make DIFFERENT queries?

Do you see where this has gotten circular? I am using different queries. I was asking if anybody knew of a trick I was unaware of to nullify the need of multiple queries.
delayedinsanity is offline  
Reply With Quote
Old 11-09-2009, 10:50 PM   #10 (permalink)
The Contributor
 
Join Date: Jun 2009
Location: Seattle, WA
Posts: 76
Thanks: 1
rguy84 is on a distinguished road
Default

wow sorry I misread the thread. No need to bite my head off
__________________
Ryan | Blog | Twitter
Send a message via AIM to rguy84 Send a message via MSN to rguy84 Send a message via Yahoo to rguy84 Send a message via Skype™ to rguy84
rguy84 is offline  
Reply With Quote
Old 11-09-2009, 11:00 PM   #11 (permalink)
is cute and cuddly
 
delayedinsanity's Avatar
 
Join Date: Mar 2008
Location: Vegas, Baby
Posts: 963
Thanks: 31
delayedinsanity is on a distinguished road
Default

Wasn't trying to bite your head off, just pointing out that we've covered those bases already.

I've tried a variety of tricks from joining the table on itself to using virtual tables and unions, but to no avail. Almost every effort skews the numbers in some way that I can't afford to permit, so I guess the consensus for now is that three queries are necessary after all.
delayedinsanity is offline  
Reply With Quote
Old 11-09-2009, 11:15 PM   #12 (permalink)
The Contributor
 
Join Date: Jun 2009
Location: Seattle, WA
Posts: 76
Thanks: 1
rguy84 is on a distinguished road
Default

Then did you look into cases like I suggested?
__________________
Ryan | Blog | Twitter
Send a message via AIM to rguy84 Send a message via MSN to rguy84 Send a message via Yahoo to rguy84 Send a message via Skype™ to rguy84
rguy84 is offline  
Reply With Quote
Old 11-09-2009, 11:40 PM   #13 (permalink)
is cute and cuddly
 
delayedinsanity's Avatar
 
Join Date: Mar 2008
Location: Vegas, Baby
Posts: 963
Thanks: 31
delayedinsanity is on a distinguished road
Default

Tried them, with the following basic query to test it;

mysql Code:
SELECT SUBSTR( timestamp, 1, 10 ) AS timestamp,
CASE WHEN spider = '' THEN COUNT( DISTINCT ip ) END AS this,
CASE WHEN spider <> '' THEN COUNT( DISTINCT ip ) END AS that
FROM `table`
GROUP BY SUBSTR( timestamp, 1, 10 )

But I just get null values in column two for row one, null in column three for row two, and so on down the line. Seems like it should work though, but the null values are counter-productive. I should be getting values for both columns on any given date (the database is populated with test data, and running the queries seperately produces the right numbers, so it isn't just because it can't find any data based on the WHERE clause).
delayedinsanity is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Unknown column in 'field list' stewart MySQL & Databases 13 05-24-2012 05:18 AM
PHP Mail - Help sending multiple attachments xperience Absolute Beginners 5 09-06-2010 08:27 AM
Search query multiple tables? (PHP, MySQL) shoaibmunir General 6 06-16-2009 01:47 PM
Where clause issue etc Orc General 12 05-08-2008 06:15 PM
Passing multiple arguments to a function via one variable. delayedinsanity Advanced PHP Programming 10 05-07-2008 05:04 AM


All times are GMT. The time now is 12:48 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design