Is it possible to combine these two queries into one ?
Meaning: Take the first row if media_group is NULL,
otherwise take all rows with the same media_group
$get_first_record = R::getRow( 'SELECT * FROM `prn` order by id asc LIMIT 1');
if(isset($get_first_record['media_group'])){
$get_media_records = R::getAll( 'SELECT * FROM `prn` WHERE media_group = '.$get_first_record['media_group']);
}
One SQL QUERY for simplify
2
Answers
We can consider using a union query along with a computed column. On MySQL:
The logic here is to retain the single record from the first half of the union in case
media_group
be null (in which case the second half of the union would not return any records). Otherwise, keep only the second half of the union.This is based on Tim’s answer but removing the need for the parameter:
Here’s a db<>fiddle.