Jump to content

[SOLVED] joining two sets of like data from different tables


Zeradin

Recommended Posts

I am trying to select the id and title from two tables and combine them together using code

but I keep getting an "ambiguous" error. How do I fix this? I know this code will only select from reviews, but I think it gets across where I'm trying to go with this:

 

'SELECT id, title FROM reviews JOIN articles ON(reviews.reviewer=articles.writer) WHERE reviews.reviewer = "'.$info['username'].'" AND visible = 1 ORDER BY timestamp DESC';

Link to comment
Share on other sites

I do have the both in both tables, actually I was looking for a way to combine them so i can sort reviews and articles together and see which is newest between the two. here is the error:

 

Error in query: SELECT id, title FROM reviews, articles WHERE reviews.reviewer = articles.writer AND reviews.reviewer = "1=1?" AND visible = 1 ORDER BY `timestamp` DESC. Column 'id' in field list is ambiguous

 

I changed the query a little to get it to work, which it didn't. Same error though.

Link to comment
Share on other sites

I want to select both. Like. I want to determine what the latest anything that someone has submitted. So i want to look in articles and reviews, sort them both by timestamp in the same list and then be able to figure out what is the newest anything they've submitted. That make sense?

Link to comment
Share on other sites

I never used union before. I tried a union that didn't look this earlier and i got errors. This one gives me this error:

 

Error in query: SELECT id, title, `timestamp` FROM((SELECT id, title, `timstamp` FROM reviews WHERE reviewer = "1=1?" AND visible = 1 ORDER BY `timestamp` DESC) UNION (SELECT id, title, `timstamp` FROM articles WHERE writer = "1=1?" AND visible = 1 ORDER BY `timestamp` DESC)). Every derived table must have its own alias

 

based on something i got from dev.mysql.com

SELECT * FROM ((

SELECT * FROM table1

WHERE ...

ORDER BY ...

LIMIT ...

) UNION (

SELECT * FROM table2

WHERE ...

ORDER BY ...

LIMIT ...

)) as t

WHERE ...

ORDER BY ...

 

What's the proper way to use union. last time i did like select id, title FROM reviews UNION articles

Link to comment
Share on other sites

sorry to be running into problems every step of the way here but

i keep running into problems.

how do i do a where clause with this? i tried one each on both sides of the union

i've tried reviewer=writer=username

i've tried reviews.reviewer=articles.writer=username

i've tried reviewer = username AND writer = username

 

this is so aggravating

thanks for helping so much though

Link to comment
Share on other sites

NICE! I almost came back with another question implementing the ORDER BY but i used google and figured it out. Kind of weird... never seen any sql query like it with order by position. in case anyone was wondering this is how i got it done:

 

'SELECT id, title, `timestamp` FROM reviews AS r WHERE r.reviewer = "'.$info['username'].'"
AND visible = 1
UNION ALL
SELECT id, title, `timestamp` FROM articles AS a WHERE a.writer = "'.$info['username'].'" 
AND visible = 1 ORDER BY 3 DESC';

 

weird.

Mchl, you're awesome for helping so much. Thanks again.

Link to comment
Share on other sites

'SELECT id, title, `timestamp` FROM reviews AS r WHERE r.reviewer = "'.$info['username'].'"
AND visible = 1
UNION ALL
SELECT id, title, `timestamp` FROM articles AS a WHERE a.writer = "'.$info['username'].'" 
AND visible = 1 ORDER BY `timestamp` DESC';

 

should work as well...

 

And check this out for extra information ;)

 

'SELECT 'Review' AS type, id, title, `timestamp` FROM reviews AS r WHERE r.reviewer = "'.$info['username'].'"
AND visible = 1
UNION ALL
SELECT 'Article', id, title, `timestamp` FROM articles AS a WHERE a.writer = "'.$info['username'].'" 
AND visible = 1 ORDER BY `timestamp` DESC';

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.