MySQL: Comparing values in diff tables SQL Query and BASH
Hi all, I'm trying to compare values in a mysql DB where 1 table has the strings(address) to search for, and 1 table has a complete list of addresses.
Table 1: id, addy1, addy2, addy3 Table 2: zip, status, addy, description, date So I need to search Table2 for all of the addresses in Table1, and if any of them match the addresses in Table1, then I need the id from Table1 & the full row that matched from Table2. I then need to fire off a php script using the id from Table1 & the status, description, & date from Table2 so I figured the best way to go about this was a bash script on a crontab. I could really use some help figuring this out as my queries were unsuccessful in accomplishing this, and my searches for answers were equally unfulfilling. Thank you for your time. |
n/m.
Spock out. |
Quote:
If I understand your table1 correctly, there are three address fields; I also guess that you need to check all three. In this case it might be better to do some normalisation and put the addresses in a separate table (let's call it table1a). Do you have the option to change the tables or is it something fixed? Can you give some typical data for both tables? My first thought would be to do the whole exercise in PHP. Is that an option? Where does cron come into the picture? Cron is, to my knowledge, used to schedule repetitive tasks; if that is what you want, cron can schedule any script as far as I know; it does not have to be bash. |
Quote:
Quote:
Quote:
Table 1 Note: ascii_addresses collation is the same as ascii with - meaning UPPERCASE CHAR instead of minus. Feel free to replace if necessary. Quote:
|
I did set this up already before you posted; based on the info that you provided I created the two tables and put some dummy data in. I did have a quick glance over the posted tables but decided not to use them for the below example.
Code:
mysql> select * from table1; Code:
mysql> select * from table2; Code:
mysql> select id,status,description,mydate from table1 join table2 on addy1=addy OR addy2=addy OR addy3=addy; To limit the dates Code:
mysql> select id,status,description,mydate from table1 join table2 on addy1=addy OR addy2=addy OR addy3=addy where mydate>date_sub(curdate(),interval 4 day); Normalisation is a standard practice; in this case it might be less efficient but if you have 10 times the same address, it's more efficient because the address only needs to be stored once. What is your part of the php code going to do? I'm not that familiar with cron; I assume that you want to run your script every e.g. 5 minutes. Somebody else can help with that. Hope this helps a bit. |
All times are GMT -5. The time now is 05:26 AM. |