Jump to content

displaying from 2 tables and order by 1 row


php_guest

Recommended Posts

I have two tables:

table One:

UserID  |  mainTitle | time

table Two:

ID  | xTitle | time

 

Time is in datetime format.

 

I would like to select rows mainTitles and time from table one and rows xTitles and time from table two. Then I would like to order all results by time.

 

I tried the following code, but it doesn't work:

$result = mysql_query("('SELECT * FROM One WHERE UserID=$ID') UNION ('SELECT * FROM Two WHERE ID=$ID') ORDER BY time");
while($row = mysql_fetch_assoc($result)) {
...
    }

Please help me how to print fields from mainTitle and xTitle in the way to be ordered by time.

 

Thanks!

Link to comment
Share on other sites

Hello,

 

I am confused about field type: UserID. Is it numeric?

 

If yes you need to remove apostrophe ' from your query. Also you have one extra apostrophe before second select UNION ('SELECT... Remove it as well.

Please not one extra closed bracket in second select statement ... WHERE ID='$ID'))

 

 

Your query should be like this:

$result = mysql_query("(SELECT * FROM One WHERE UserID=$ID) UNION (SELECT * FROM Two WHERE ID=$ID)) ORDER BY time");

:)

 

Don't forget union method request the same number of fields in both tables. :-\

 

Cheers ;)

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.