I’m looking for a mysql query (using php) to go through each table one at a time and only display results that are different than those in cross_check.lastmod
t_one
guid | name | lastmod
1 | Joe | 2012-01-01 01:00:00
2 | Tom | 2012-01-02 01:00:00
3 | Sue | 2012-03-01 02:00:00
t_two
guid | pet | lastmod
4 | cat | 2012-01-01 01:00:00
5 | dog | 2012-01-02 01:00:00
6 | fish | 2012-03-01 02:00:00
t_three
guid | fruit | lastmod
7 | orange | 2012-01-01 01:00:00
8 | pear | 2012-01-02 01:00:00
9 | grape | 2012-03-01 02:00:00
cross_check
guid | lastmod
1 | 2012-01-01 01:00:00
2 | 2012-01-02 01:00:00
3 | 2012-01-01 02:00:00
4 | 2012-01-01 01:00:00
5 | 2012-01-02 01:00:00
6 | 2012-01-01 02:00:00
7 | 2012-01-01 01:00:00
8 | 2012-01-02 01:00:00
9 | 2012-01-01 02:00:00
The query results would be:
t_one => 3 | Sue | 2012-03-01 02:00:00
t_two => 6 | fish | 2012-03-01 02:00:00
t_three => 9 | grape | 2012-03-01 02:00:00
My code so far (needs the table cross reference)
$tables = array('t_one', 't_two', 't_three');
foreach ($tables AS $table) {
$query = sprintf("SELECT * FROM '%s'",
mysql_real_escape_string($table));
$result = mysql_query($query);
}
Just
JOINin thecross_checktable.Still one query per table, though (you can fart around with unions but that doesn’t reduce the core number of queries).
If you wanted to combine them into one, you could use UNIONs:
I feel like this isn’t all that more efficient than doing three different queries (a gut feel though, unsubstantiated claim).
You could also do (although this only works if there’s no crossover between the
t_*guids; if there ist_onewill be favoured, thent_two):You could also put
COALESCE(t_one.name, t_two.pet, t_three.fruit) AS labelif you wanted to grab the labels as opposed to the ids.