TutorMe Posted September 3, 2007 Share Posted September 3, 2007 Is there a way to select from multiple mysql tabels? Quote Link to comment https://forums.phpfreaks.com/topic/67813-solved-select-from/ Share on other sites More sharing options...
pocobueno1388 Posted September 3, 2007 Share Posted September 3, 2007 Yes, there is. Quote Link to comment https://forums.phpfreaks.com/topic/67813-solved-select-from/#findComment-340807 Share on other sites More sharing options...
TutorMe Posted September 3, 2007 Author Share Posted September 3, 2007 Ok. If I have this: $result = mysql_query("SELECT * FROM January ORDER BY day"); How would I select from more than just January? Quote Link to comment https://forums.phpfreaks.com/topic/67813-solved-select-from/#findComment-340814 Share on other sites More sharing options...
pocobueno1388 Posted September 3, 2007 Share Posted September 3, 2007 Well, you are going to need a unique key in each table that relate to eachother first. Could you give more information on both tables? Quote Link to comment https://forums.phpfreaks.com/topic/67813-solved-select-from/#findComment-340817 Share on other sites More sharing options...
corbin Posted September 3, 2007 Share Posted September 3, 2007 If you just want to select from multiple tables, it's as easy as: SELECT something FROM table1, table2 But, chances are, that's quite useless.... Let's say you have something like this: table users: id name password and then you have another table called friends: user_id friend_id To get the name of the friend, you could do something like: SELECT f.friend_id, u.name FROM friends as f INNER JOIN users as u ON u.id = f.friend_id WHERE f.user_id = 1 That would return the friend IDs and names for the user ID 1. Quote Link to comment https://forums.phpfreaks.com/topic/67813-solved-select-from/#findComment-340818 Share on other sites More sharing options...
TutorMe Posted September 3, 2007 Author Share Posted September 3, 2007 To: pocobueno: I'm not sure exactly what you mean. I created each table with the following code: CREATE TABLE `*Month` ( `username` VARCHAR( 20 ) NOT NULL , `month` VARCHAR( 13 ) NOT NULL , `day` INT( 2 ) NOT NULL , `year` INT( 4 ) NOT NULL ) TYPE = MYISAM ; *Month being replaced by whichever month's table I was creating. Is that what you were asking for? To: Corbin: Each table has identical rows, so I tried the following: $result = mysql_query("SELECT * FROM January, February, etc... ORDER BY day"); However, it just turned up blank. I appreciate your replies. I think I'm overlooking something. Quote Link to comment https://forums.phpfreaks.com/topic/67813-solved-select-from/#findComment-340824 Share on other sites More sharing options...
darkfreaks Posted September 3, 2007 Share Posted September 3, 2007 <?phpresult = mysql_query("SELECT * FROM January,February ORDER BY day");?> Quote Link to comment https://forums.phpfreaks.com/topic/67813-solved-select-from/#findComment-340827 Share on other sites More sharing options...
TutorMe Posted September 3, 2007 Author Share Posted September 3, 2007 <?php("SELECT January, February *FROM content ORDER BY day";?> That code worked somewhat, but only when I removed the ORDER BY part. However, when I did this it only showed information from February. Quote Link to comment https://forums.phpfreaks.com/topic/67813-solved-select-from/#findComment-340833 Share on other sites More sharing options...
darkfreaks Posted September 3, 2007 Share Posted September 3, 2007 instead of day try Order by ASC or Desc Quote Link to comment https://forums.phpfreaks.com/topic/67813-solved-select-from/#findComment-340838 Share on other sites More sharing options...
darkfreaks Posted September 3, 2007 Share Posted September 3, 2007 do you havew a column in the database with a the days in the month? Quote Link to comment https://forums.phpfreaks.com/topic/67813-solved-select-from/#findComment-340843 Share on other sites More sharing options...
TutorMe Posted September 3, 2007 Author Share Posted September 3, 2007 That didn't seem to help. I appologize for the difficulty. Yes, I do have that row, but what the code returns almost looks like I don't. On another page, the code works with only one month and WITH the ORDER BY day, so i don't know what to do. Quote Link to comment https://forums.phpfreaks.com/topic/67813-solved-select-from/#findComment-340851 Share on other sites More sharing options...
pocobueno1388 Posted September 3, 2007 Share Posted September 3, 2007 Take a look at this tutorial: http://www.tizag.com/sqlTutorial/sqljoin.php Quote Link to comment https://forums.phpfreaks.com/topic/67813-solved-select-from/#findComment-340853 Share on other sites More sharing options...
TutorMe Posted September 3, 2007 Author Share Posted September 3, 2007 Thanks for the link, but I actually need to keep the tables separate, but want to display information from all of them in one table. Here is an example using ORDER BY day, but with a different database: http://tutorme.homeip.net/bdays/index.php. It works. Keep in mind though, that a. there is not much info there, and b. it is displaying each table in a separate table. Quote Link to comment https://forums.phpfreaks.com/topic/67813-solved-select-from/#findComment-340858 Share on other sites More sharing options...
darkfreaks Posted September 3, 2007 Share Posted September 3, 2007 try ORDER BY days ASC Quote Link to comment https://forums.phpfreaks.com/topic/67813-solved-select-from/#findComment-340859 Share on other sites More sharing options...
TutorMe Posted September 3, 2007 Author Share Posted September 3, 2007 That didn't work either. Could it have anything to do with the way my test server is setup? Quote Link to comment https://forums.phpfreaks.com/topic/67813-solved-select-from/#findComment-340864 Share on other sites More sharing options...
corbin Posted September 3, 2007 Share Posted September 3, 2007 Hmmmm..... You could try something like: SELECT * FROM January, February, March, April.... Assuming you had something like January ------------ corbin jan 3 1991 TutorMe jan 15 1985 SomeOne jan 15 1985 February -------------- SomeOneElse feb 1 1920 some1 feb 23 1995 someoneelse feb 25 1990 It would return something like: corbin jan 3 1991 TutorMe jan 15 1985 SomeOne jan 15 1985 SomeOneElse feb 1 1920 some1 feb 23 1995 someoneelse feb 25 1990 Quote Link to comment https://forums.phpfreaks.com/topic/67813-solved-select-from/#findComment-340865 Share on other sites More sharing options...
TutorMe Posted September 3, 2007 Author Share Posted September 3, 2007 This is the wierdest thing. I tried your code corbin (with january, february, and march), and it only showed info from march, and then, all the info in march was shown twice. I'm baffled. Quote Link to comment https://forums.phpfreaks.com/topic/67813-solved-select-from/#findComment-340880 Share on other sites More sharing options...
darkfreaks Posted September 3, 2007 Share Posted September 3, 2007 SELECT January, February, etc FROM January, February, etc Quote Link to comment https://forums.phpfreaks.com/topic/67813-solved-select-from/#findComment-340892 Share on other sites More sharing options...
corbin Posted September 3, 2007 Share Posted September 3, 2007 My bad..... My format was entirely incorrect... When you do SELECT * FROM table1, table2 it selects the columns from table1 and table2, not all of the rows.... bleh... sec Edit: Ahhh.... After some thinking, I came up with: SELECT * FROM jan UNION SELECT * FROM feb UNION SELECT * FROM march UNION SELECT.... blah blah blah Quote Link to comment https://forums.phpfreaks.com/topic/67813-solved-select-from/#findComment-340916 Share on other sites More sharing options...
darkfreaks Posted September 3, 2007 Share Posted September 3, 2007 yeah i just looked it up before you posted there. was ifxing to recomend it but you beat me to it Please hit the solve button Quote Link to comment https://forums.phpfreaks.com/topic/67813-solved-select-from/#findComment-340923 Share on other sites More sharing options...
TutorMe Posted September 3, 2007 Author Share Posted September 3, 2007 Wow. Thank you guys so much. I thought it was hopeless. This did exactly what I was wanting to do. Quote Link to comment https://forums.phpfreaks.com/topic/67813-solved-select-from/#findComment-340952 Share on other sites More sharing options...
pocobueno1388 Posted September 3, 2007 Share Posted September 3, 2007 Don't forget to press "solved" Quote Link to comment https://forums.phpfreaks.com/topic/67813-solved-select-from/#findComment-340954 Share on other sites More sharing options...
TutorMe Posted September 4, 2007 Author Share Posted September 4, 2007 Sorry. Thanks Paco. Quote Link to comment https://forums.phpfreaks.com/topic/67813-solved-select-from/#findComment-340988 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.