Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

MySQL Query Syntax

Posted on 2008-06-18
4
Medium Priority
?
624 Views
Last Modified: 2013-12-12
I have the following query:

SELECT a.venuename, a.venueid, b.stars, b.reviewtext, AVG( stars ) AS average, sum(b.total) AS total
FROM venue a, venuereview b
WHERE a.venueid = b.venueid
AND b.id = 50
AND b.switch = '1'
GROUP BY b.venueid
ORDER BY average DESC


What I want to do is display the review with the ID of 50 first, then display the rest of the reviews for the venueid related to the id - 50.

Can anyone help me out?
0
Comment
Question by:lvollmer
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 21815562
if I understand correctly:
SELECT a.venuename, a.venueid, b.stars, b.reviewtext, b.id, AVG( stars ) AS average, sum(b.total) AS total
FROM venue a, venuereview b
WHERE a.venueid = b.venueid
AND b.switch = '1'
GROUP BY b.venueid
ORDER BY CASE WHEN b.id = 50 THEN 0 ELSE 1 END ASC, average DESC

Open in new window

0
 

Author Comment

by:lvollmer
ID: 21815744
Angel - that gave me all reviews.

here is an example of what I need.

reviewID-50 has a venueID of 900

venueID900 has 4 reviews

I want to display those 4 reviews, with the first review displaying reviewID-50
0
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 2000 total points
ID: 21815775
so, when a VenueID has at least 1 reviewID = 50, then show them all for that VenueID
SELECT a.venuename, a.venueid, b.stars, b.reviewtext, b.id, AVG( stars ) AS average, sum(b.total) AS total
FROM venue a
JOIN venuereview b
  ON a.venueid = b.venueid
 AND b.switch = '1'
WHERE exists ( SELECT NULL 
                 FROM venuereview i
                WHERE a.venueid = i.venueid
                  AND i.id = 50
             )
GROUP BY b.venueid
ORDER BY CASE WHEN b.id = 50 THEN 0 ELSE 1 END ASC, average DESC

Open in new window

0
 

Author Comment

by:lvollmer
ID: 21815877
slight modification and it works - thanks!

SELECT a.venuename, a.venueid, b.stars, b.reviewtext, b.id, AVG( stars ) AS average, sum( b.total ) AS total
FROM venue a
JOIN venuereview b ON a.venueid = b.venueid
AND b.switch = '1'
WHERE EXISTS (

SELECT NULL
FROM venuereview i
WHERE a.venueid = i.venueid
AND i.id =49
)
GROUP BY b.id
ORDER BY CASE WHEN b.id =49
THEN 0
ELSE 1
END ASC , average DESC
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
By, Vadim Tkachenko. In this article we’ll look at ClickHouse on its one year anniversary.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question