0

I have a short question an I am stuck at this point. I have the following query:

SELECT A, B
FROM my_table AS r
LEFT JOIN my_table_2 AS p ON r.rs_id = p.rs_id
WHERE r.rs_id = $DB->quote($_SESSION['mysession']['id']) EXISTS (SELECT A, B, date FROM my_table WHERE date = CURDATE())';

What I am trying to do is to get results from the database. If there is a row however that has a record that matches the current date I only want to get this rows, so not all A + B anymore. If there are NO rows with a current date I want to load all A + B by default.

Does anyone knows the answer? Thanks!

grepsedawk
  • 3,324
  • 2
  • 26
  • 49
Michael
  • 37
  • 6

1 Answers1

1

To do this in a clean way, you would check on application level, if the query searching for records with date equal to current date returns any records. If not, execute a second query.

You can do the same in MySQL, but since this is in a way a bit "dirty" (the techniques used are not meant to be used this way), I would recommend solving doing this on application level. In case you're curious, here's how:

SELECT SQL_CALC_FOUND_ROWS whatever
FROM table
WHERE date_column = CURDATE()
UNION ALL
SELECT whatever
FROM table
WHERE FOUND_ROWS() = 0;
  • read more about it here
fancyPants
  • 50,732
  • 33
  • 89
  • 96