I have a SELECT
statement which is successfully populating the data I need within the following table. However I want to filter down the results with an if statement. Basically I want to have it where:
If there is a value for Timestamp
and/or msg
get results else leave blank.
<?php foreach ( $result as $query ){ ?>
<tr>
<td><?php echo $query->text1; ?></td>
<td><?php echo $query->text2; ?></td>
<td><?php echo $query->text3; ?></td>
<td><?php if($query->Timestamp!=""){echo date('m/d/y', strtotime($query->Timestamp)); }?></td>
<td><?php echo $query->msg; ?></td>
</tr>
<?php }?>
How would I write this if statement into this code in order to only show rows that produced either a date
or a msg
or both?
As requested I have added the initial query though a bit intricate hence I was hoping to avoid messing with it any further.
$result = $wpdb->get_results(
"SELECT DISTINCT text3,
CASE WHEN text3 LIKE '%S' THEN S_Msg
WHEN text3 LIKE '%N' THEN N_Msg
ELSE E_Msg
END AS msg,
CASE WHEN text3 LIKE '%S' THEN S_Time
WHEN text3 LIKE '%N' THEN N_time
WHEN text3 LIKE '%E' THEN E_time
END AS Timestamp,
value1
FROM table_1 AS a
INNER JOIN table_2 AS b
ON a.Value2 = b.Value3
WHERE value1='$value1'");
I am open to suggestions for either solution that works :)
I have tried using the suggestion made by M Khalid Junaid
having (msg is not null and msg <> '')
or (Timestamp is not null and Timestamp <> '')
however this causes issues with other queries no longer loading as I think due to me not using groups but multiple different queries on the page and this is just one of them within a bigger set of queries.
I fear this one is over my head. to replicate this on a sqlfiddle i would need to setup 3 database tables so will need a day to prepare that unless someone has another solution. I am still open to the cop out of a if statement as well lol
You can add an AND statement at the end that checks Timestamp or N_msg as not being empty
$result = $wpdb->get_results(
"SELECT DISTINCT text3,
CASE WHEN text3 like '%S' THEN S_Msg
WHEN text3 like '%N' THEN N_Msg
ELSE E_Msg
END AS msg,
CASE WHEN text3 LIKE '%S' THEN S_Time
WHEN text3 LIKE '%N' THEN N_time
WHEN text3 LIKE '%E' THEN E_time
END AS Timestamp,
value1
FROM table_1 AS a
INNER JOIN table_2 AS b
ON a.Value2 = b.Value3
WHERE value1='$value1'
AND N_msg != '' OR S_time != '' OR N_time != '' OR E_time != ''");
To get the results if any criteria is met you can use following
select distinct text3,
case when text3 like '%S' then S_Msg when text3 like '%N' then N_Msg else E_Msg end as msg,
case when text3 like '%S' then S_Time when text3 like '%N' then N_time when text3 like '%E' then E_time end as Timestamp,
value1
from table_1 as a
inner join table_2 as b on a.Value2 = b.Value3
where value1='$value1'
having (msg is not null and msg <> '')
or (Timestamp is not null and Timestamp <> '')
Not sure about how your data is saved in DB as NULL
or with empty so i added both filters. Purpose to use having
clause is to apply filter on custom aliases
You could probably do it like this - using the alias in a having
clause
select distinct
text3,
case
when text3 like '%S' then S_Msg
when text3 like '%N' then N_Msg
else E_Msg
end as msg,
case
when text3 like '%S' then S_Time
when text3 like '%N' then N_time
when text3 like '%E' then E_time
end as Timestamp,
value1
from table_1 as a
inner join table_2 as b on a.Value2 = b.Value3
having value1='$value1' and msg is not null and timestamp is not null