jimmyoneshot Posted December 10, 2010 Share Posted December 10, 2010 I'm struggling to get a cross join query working in my script. At the moment I have 2 tables in a mysql database - merchants and vouchers. The relationship is each merchant has many vouchers i.e. one to many. The fields of the merchant table are:- merchant_id (Primary Key), merchant name and the fields in the vouchers table are:- voucher_id (Primary Key), merchant_id (Foreign key from the merchants table to signify which merchant the voucher is related to), voucher_name I'm trying to implement a feature which once the user clicks a certain letter all vouchers that belong to any merchants whose merchant_name begins with the clicked letter will be shown but I can't seem to get it to work with the following query and am unsure as to where I'm going wrong:- SELECT m.merchant_name, v.* FROM merchants AS m CROSS JOIN vouchers AS v USING(merchant_id) WHERE m.merchant_name like '$letter%' $letter is a vriable that stores the letter that was clicked by the user. Can anyone suggest anything? Thanks for the help in advance Quote Link to comment https://forums.phpfreaks.com/topic/221223-need-help-with-a-cross-join-query/ Share on other sites More sharing options...
Adam Posted December 10, 2010 Share Posted December 10, 2010 I'd use a normal join here: select m.merchant_id , m.merchant_name , v.voucher_id , v.voucher_name from vouchers v join merchants m on (m.merchant_id = v.merchant_id) where m.merchant_name like '{$letter}%' Quote Link to comment https://forums.phpfreaks.com/topic/221223-need-help-with-a-cross-join-query/#findComment-1145385 Share on other sites More sharing options...
jimmyoneshot Posted December 10, 2010 Author Share Posted December 10, 2010 That works great. Thanks Mr Adam. Quote Link to comment https://forums.phpfreaks.com/topic/221223-need-help-with-a-cross-join-query/#findComment-1145396 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.