Can anyone help me with a MySQL query I’m trying to put together. I have two tables as follows (cut down just for the example).
uk_postcodes
____________
Pcode | County | Region
AB10 | Aberdeen | Scotland
AB11 | Aberdeen | Scotland
B10 | West Midlands | West Midlands
p_venues
________
id | Venue_name | address_postcode
1 | Samples venue | AB10 1AT
2 | Samples venue | B10 2DZ
Quite simply, I’m trying to select the Region from table uk_postcodes based on the corresponding postcode (address_postcode) in the p_venues table.
I’ve tried Like% and REGEXP ^ but can’t seem to get it to work.
This is what I’ve tried…
SELECT * FROM `uk_postcodes`, `p_venues`
WHERE `p_venues`.address_postcode REGEXP ^ `uk_postcodes`.Pcode
This would be easy if I could specifically supply data hardcoded but the problem lies with the comparison (uk_postcodes.Pcode) in the 2nd part of the query being a field – I can’t seem to get the syntax right.
Can anyone please help??
Thanks
This ought to do it. You need to concatenate the
LIKEwildcard to the Pcode value from uk_postcodes. If any of your columns are fixed length text, you may need to trim trailing spaces first.