Jump to content

[SOLVED] Select From


TutorMe

Recommended Posts

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.

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.