Problem
I’m joining two tables on postcode. I only have the data for the first half. The problem is where I have CM16 6BY in my user table and it matches CM1 and CM16 in my postcode table. Obviously I only want it to match with CM16 but I cannot write a query to do so. I’m sure I am doing something stupid but I am stuck. Can anyone help? Many thanks.
Query
SELECT user.postcode, postcode.postcode
FROM user
INNER JOIN postcode ON user.postcode LIKE CONCAT( postcode.postcode, "%" )
WHERE user.postcode LIKE "CM%"
LIMIT 0 , 30
Results
postcode postcode
CM16 6BY CM1
CM16 6BY CM16
CM3 4XB CM3
CM9 6SW CM9
CM24 8JD CM2
CM24 8JD CM24
CM3 3EP CM3
CM1 3ES CM1
CM2 5AP CM2
CM22 6AZ CM2
CM22 6AZ CM22
CM17 9LH CM1
CM17 9LH CM17
CM2 7ER CM2
CM159ES CM1
CM159ES CM15
CM15 0NW CM1
CM15 0NW CM15
CM3 6XA CM3
CM14 4BT CM1
CM14 4BT CM14
CM9 4TB CM9
CM1 1HZ CM1
CM22 6DG CM2
CM22 6DG CM22
CM15 8QY CM1
CM15 8QY CM15
CM8 3HR CM8
CM1 1GS CM1
CM5 0BB CM5
try this out