Basically I have a site that offers premium users better position on the search features, and then the other users will be after the premium users:
CREATE TABLE IF NOT EXISTS `mlisting` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `cid` int(11) NOT NULL,
  `title` varchar(255) NOT NULL,
  `price` decimal(9,2) NOT NULL,
  `images` text NOT NULL,
  `live` tinyint(1) NOT NULL DEFAULT '1',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=5 ;
INSERT INTO `mlisting` (id, cid, title, price, images, live) VALUES 
(1, 1, 'title 1', 29.99, '', 1),
(2, 2, 'title 2', 69.99, '', 1),
(3, 2, 'title 3',  1.99, '', 1),
(4, 1, 'title 4', 48.99, '', 1);
CREATE TABLE IF NOT EXISTS `companies` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `trust` datetime NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=3 ;
INSERT INTO `companies` (id, trust) VALUES 
(1, '1970-01-01 00:00:00'),(2, '2019-01-01 00:00:00');
So the query should search where premiumdate >= todaysdate (premium expiry day) and then once no more results are found to then use the rest in the table.
I have tried UNION but that did not work on the order by, the premium users did not get priority listing.
Here is my query so far:
(
SELECT 
m.id, m.cid, m.title, m.price, m.images, c.trust 
FROM 
mlisting m 
LEFT JOIN 
companies c 
ON (m.cid = c.id) 
WHERE c.trust >='{$itsToday}' AND m.live=1 
ORDER BY m.id DESC
) 
UNION 
(
SELECT 
mo.id, mo.cid, mo.title, mo.price, mo.images, co.trust 
FROM 
mlisting mo 
LEFT JOIN 
companies co ON (mo.cid = co.id) 
WHERE co.trust='1970-01-01 00:00:00' AND mo.live=1 
ORDER BY mo.id DESC
)
Thanks for taking a look and hopefully pulling me in the right direction.
Here is a SQL Fiddle to show what I mean by the ORDER BY not working: SQL Fiddle