Erwin007 Posted May 8, 2023 Share Posted May 8, 2023 $query = "SELECT * FROM `sales` WHERE rep_id = $query = "SELECT * FROM `reps` WHERE rep_touroperatorid = '5' I have difficulty explaining it but will try: I need to select all sales where all the reps have the rep_touroperatorid 5 How to "join" or "insert into" or ?? If somebody can help, would be great. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/316274-sql-creation-problem/ Share on other sites More sharing options...
Barand Posted May 8, 2023 Share Posted May 8, 2023 It helps us immensely if you tell us your table structures when asking about joins Quote Link to comment https://forums.phpfreaks.com/topic/316274-sql-creation-problem/#findComment-1608202 Share on other sites More sharing options...
Erwin007 Posted May 8, 2023 Author Share Posted May 8, 2023 Table reps: - rep_id - rep_name - rep_touroperatorid Tablse sales: - sales_id - sales_repid - sales_ticketnr Hope this helps Quote Link to comment https://forums.phpfreaks.com/topic/316274-sql-creation-problem/#findComment-1608203 Share on other sites More sharing options...
Solution Barand Posted May 8, 2023 Solution Share Posted May 8, 2023 Try SELECT rep_id , rep_name , sales_id , sales_ticketnr FROM reps r JOIN sales s ON r.rep_id = sales_rep_id WHERE rep_touroperatorid = 5 Quote Link to comment https://forums.phpfreaks.com/topic/316274-sql-creation-problem/#findComment-1608204 Share on other sites More sharing options...
Erwin007 Posted May 8, 2023 Author Share Posted May 8, 2023 Yes! Works like a charm. Thank you so much, learning a lot in 2 days. 😀👍 Quote Link to comment https://forums.phpfreaks.com/topic/316274-sql-creation-problem/#findComment-1608205 Share on other sites More sharing options...
Erwin007 Posted May 8, 2023 Author Share Posted May 8, 2023 I celebrated too fast.... what exactly is "reps r", "sales s" and r.reps_id" in:? FROM reps r JOIN sales s ON r.rep_id = sales_rep_id Quote Link to comment https://forums.phpfreaks.com/topic/316274-sql-creation-problem/#findComment-1608207 Share on other sites More sharing options...
Barand Posted May 8, 2023 Share Posted May 8, 2023 The "r" and the "s" are table aliases, not realy neccessary here (force of habit) but are useful if you have columns in different table with the same name. Suppose reps and sales both had columns called "id"; you could then differentiate between them by either reps.id and sales.id or by using the aliases r.id and s.id, which is quicker to write. Occasionally you may wish to reference the same table twice or more in a query in which case the alias is essential EG TABLE : people ------------------------------ id int primary key name varchar father_id int foreign key mother_id int foreign key ------------------------------ SELECT p.id , p.name as person , f.name as father , m.name as mother FROM people p JOIN people f ON p.father_id = f.id JOIN people m ON p.mother_id = m.id Quote Link to comment https://forums.phpfreaks.com/topic/316274-sql-creation-problem/#findComment-1608208 Share on other sites More sharing options...
Erwin007 Posted May 9, 2023 Author Share Posted May 9, 2023 Yes, it worked, thanks again. Quote Link to comment https://forums.phpfreaks.com/topic/316274-sql-creation-problem/#findComment-1608210 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.