Jump to content

Nested SELECTS, to limit results


PC Nerd

Recommended Posts

hi,

 

Im trying to create a veiw that matches up the most recent "comment" to the article.  Theyve currently called "approvals" because its for the admin to approve/decline etc articles before they are posted on teh site. 

 

Here is my SQL:

 

 

CREATE VIEW `Article_Select` AS  SELECT (

SELECT User_Name

FROM Users

WHERE User_ID = Articles.Author

) AS Author, Articles.Author AS Author_ID, Articles.ID, Articles.Title, Articles.Content, Articles.Date, Articles.Updated, Articles.Type, Article_Approvals.Date AS Approval_Date, Article_Approvals.reason, Article_Approvals.status

FROM `Articles` , `Article_Approvals`

WHERE Articles.ID = Article_Approvals.Article_ID

AND Articles.ID = (

SELECT Article_approvals.Approval_ID

FROM Article_Approvals

WHERE Article_Approvals.Article_ID = ID

ORDER BY Article_Approvals.Date ASC LIMIT 1)

 

 

Im not sure exactly how to explain it... however teh Article_Approvals Table can contain multiple records for a single article..... so that if an admin changes they can simply re-approve/decline it. the status field stores -1, 1, or 2.  -1 is declined perminently ( ie off topic, useless/inappropriate etc), 1 is approved and 2 means that it needs editing for approval.

 

I want the view to show the status adn reason for the most recent approval.... however im slightly confused as to how to do this with nested inserts.

 

Essentially:

 

SELECT <article_fields>, <Most recent Approval Fields> FROM `Articles`, `Article_Approvals`

 

 

Thanks for your help.

 

 

Link to comment
Share on other sites

no, don't nest select queries coz they wont work.

 

you can use brackets () to nest conditions in the same query or use multi-table selects in SQL... you can even get one result from SQL, bung it in an array and then loop through the array to get another SQL select query to work.

Link to comment
Share on other sites

Bump - ill re explain.

 

I have 2 tables.  Articles, and Article_Approvals.

 

I want to join the two tables into one view....

There may be more than one record in Article_Approvals that matches Articles.

Article Approvals has a date.

 

 

I want to match up the two, based on the most recent record in Article Approvals.

 

now to receive a single row its

SELECT <whatever> FROM Article_Approvals ORDER BY Date LIMIT 1 - that will get the most recent....

 

how can i do a similar thing for every row on the other table?

 

Thanks

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.