johnnasx
johnnasx

Reputation: 168

mysql if column empty select, if not empty apply conditions

I'm having some difficulties on applying certain conditions if a column is empty or not.

My table is as follows:

CREATE TABLE `meets` (
    `id` INT,
    `scheduled` VARCHAR(255),
    `status` INT
);

INSERT INTO meets(id,scheduled,status) VALUES (1,'','1');
INSERT INTO meets(id,scheduled,status) VALUES (2,'','2');
INSERT INTO meets(id,scheduled,status) VALUES (2,'1613220631','3'); // in 30 minutes
INSERT INTO meets(id,scheduled,status) VALUES (2,'1644756631','3'); // 2022

What I did so far is next:

SELECT * FROM meets WHERE FROM_UNIXTIME(scheduled) BETWEEN DATE_SUB(NOW(), INTERVAL 30 MINUTE) AND DATE_SUB(NOW(), INTERVAL -30 MINUTE) ORDER BY `id` DESC

The above only selects the record that has a timestamp in the following/past 30 minutes.

Other than that record, I also need to select record id 1 because it has status == 1.

So basically

if scheduled column is empty, check for status to be 1 and select if true;
if scheduled column is timestamp, apply condition from the query posted above; 

Any ideas? Thank you!

Upvotes: 1

Views: 95

Answers (1)

ScaisEdge
ScaisEdge

Reputation: 133360

You could add the missing rows using union

    SELECT * 
    FROM meets 
    WHERE FROM_UNIXTIME(scheduled) 
    BETWEEN DATE_SUB(NOW(), INTERVAL 30 MINUTE) AND DATE_SUB(NOW(), INTERVAL -30 MINUTE) 
    ORDER BY `id` DESC
    UNION 
    select * from meets where scheduled = '' AND status = 1

Upvotes: 1

Related Questions