I have a large table on a remote server and want to keep a copy on the local machine in a flat disk file. If I keep a record of a serial number locally read from the first row of the table then can I issue a SELECT command to return all of the rows in the table if the contents of the column at row id=0 <> my locally stored serial number?
Something like
SELECT * FROM 1_makes WHERE IF id@0<> local_serial
Hope that makes sense
delimiter $$
CREATE TABLE `1_makes` (
`id` int(11) NOT NULL,
`make` varchar(20) DEFAULT NULL,
`allow_global_disc` tinyint(4) DEFAULT NULL,
`home_text` text,
`pack_home_text` text,
`update_ref` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8$$
So what I want is that the entire tables is returned only if the contents of row with id=0 and update_ref is different to the locally stored value.
If I get results then I know the data has changed so I can save it to a local disk file and update the read update_ref
Looks like this is what I need
I have removed the column update_ref and put the update value in the make column @ id=0
SELECT * FROM 1_makes WHERE (SELECT IF(make <> ‘my update number’, 1, 0) FROM 1_makes WHERE id = 0)
This works, just wanted to ask what the numbers 1, 0 relate to in IF(make <> ‘my update number’, 1, 0)
Your case would probably be better served by issuing a separate query. The application would first query the value of the row where id=0, and then proceed based on that.
However, you could accomplish this with a subquery:
Or an INNER JOIN:
However, if you’re just testing to see if there are results, it’s best just to return the count rather than return the unused values:
Or perhaps even better, use LIMIT to stop after the first row:
Or even better, just use the return value of the IF:
Even better, why store a value for update_ref for every row? Why not move it to its own table:
The IF statement return 0, which means false, or 1, which means true.
Finally, if the id is an incremental value, why not just search for rows where the id is larger than the stored last value instead:
Also, this doesn’t account for updates. Only, inserts.
Consider adding a slave server that is replicated from the master server. Then, simply automate backing up the slave server. This is how most backups are done.