bokivaol Posted October 10, 2010 Share Posted October 10, 2010 What is command to connect two tables from the database. I would like to select from table "newsletter" all emails which are different AND to select from the table "user_registration" all users with newsletter=1, but if there any user in those two tables with the same email, I would not like to display it twice. When I put in the code $s = dbQuery("select * from `newsletter` where `email`<>'' order by `email`"); - it display all different emails from the table "newsletter" When I put in the code $s = dbQuery("select * from `user_registration` where `newsletter`='1' order by `email`"); - it display all emails from the table "user_registration" with newsletter=1. I would like to connect these two equations, but I don't know how, I don't know what is command? Can you help me about this? I used JOIN command, but I didn't do anything. <?php $s = dbQuery("select * from `user_registration` where `newsletter`='1' order by `email`");........1 /* $s = dbQuery("select * from `newsletter` where `email`<>'' order by `email`");........................2*/ $count = dbNumRows($s); if($count>0){ ?> <select size="6" multiple="multiple" id="multi" name="multi[]" style="width:200px;"> <?php while($t = dbFetchArray($s,MYSQL_BOTH)) { ?> <option value="<?php echo $t["id"]; ?>"><?php echo $t["email"]; ?></option> <?php } ?> <?php } ?> </select> <input type="checkbox" onClick="selectAll(this);"/> Select All<br /> <input type="submit" onclick="return checkdel();" name="DelUser" value="Delete Those Subscribers" /> <?php } else { ?> No Records! <?php } ?> Can anyone help me to write a code, I am not so good in mysql and php, but I know what I want. [attachment deleted by admin] Quote Link to comment https://forums.phpfreaks.com/topic/215544-how-to-select-from-two-tablse-from-the-database/ Share on other sites More sharing options...
fenway Posted October 10, 2010 Share Posted October 10, 2010 You're looking for a join. Quote Link to comment https://forums.phpfreaks.com/topic/215544-how-to-select-from-two-tablse-from-the-database/#findComment-1120798 Share on other sites More sharing options...
bokivaol Posted October 10, 2010 Author Share Posted October 10, 2010 Maybe it is easy for you, but for me it is very hard. I tried with SELECT newsletter.email, user_registration.email FROM user_registration LEFT JOIN newsletter ON newsletter.email <> user_registration.email , but I need function IF and THEN. IF they are = THEN display just ONE of them, all other different emails should be displayed. Quote Link to comment https://forums.phpfreaks.com/topic/215544-how-to-select-from-two-tablse-from-the-database/#findComment-1120820 Share on other sites More sharing options...
bokivaol Posted October 10, 2010 Author Share Posted October 10, 2010 I solved problem. I used UNION function: "(SELECT `email` FROM `user_registration` WHERE `newsletter`='1') UNION (SELECT `email` FROM `newsletter`) ORDER BY `email`" Quote Link to comment https://forums.phpfreaks.com/topic/215544-how-to-select-from-two-tablse-from-the-database/#findComment-1120834 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.