Jump to content

Recommended Posts

I'm trying to offload some of my PHP control structure "workload" to MySQL for scalability.

Here is a pseudocode example of what I'm trying to do

 

SELECT the first 20 records from event_dates that have a date>today OR the next three weeks, whichever is the larger set

 

I know how I'd write those queries individually, but not sure how to combine them to avoid having to grab 2 sets, waste time comparing them via PHP, and throw one set away.

 

(MySQL 5.0 as part of WAMP)

Link to comment
https://forums.phpfreaks.com/topic/165963-solved-more-complicated-conditionals/
Share on other sites

Ok, I found my solution on my own, and it's the "UNION" statement.  I assumed that UNION would return duplicate rows if the results overlap.  It does not.

It's pretty simple really.  You just UNION both queries (one by date and one by quantity) and you'll wind up with the larger of the two as the final result.

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.