473,503 Members | 4,234 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Sorting results from one table by rows in another table

HaLo2FrEeEk
404 Contributor
I've got a table where I store tips for a challenge. Its structure is this:

tip_id, challenge_id, user_id, ip_address, tip_date, tip_text

The tips can be voted up or down, I store vote data in another table, which looks like this:

tip_id, ip_address, vote_date, vote

The similar column between the two is tip_id. Vote is a bool, 0 means a vote down, 1 means a vote up.

My page will initially display one tip per challenge, but there will be a page that will show all tips for the specified challenge. In an effort to reduce my query count, I devised this query:

Expand|Select|Wrap|Line Numbers
  1. SELECT
  2.   V.vote AS vote,
  3.   T.tip_id AS tip_id,
  4.   T.tip_date AS tip_date,
  5.   T.tip_text AS tip_text
  6.   FROM votes V
  7.     LEFT JOIN tips T
  8.     ON V.tip_id = T.tip_id
  9.   WHERE T.challenge_id = 1
  10.   ORDER BY T.tip_id ASC
Which returns a new row for each vote and a copy of the tip data for each one. As an example, I inserted a test tip and 3 test votes, running this query returns 3 rows with the 3 votes, and 3 copies of the tip data, and I know that's how it's supposed to work. What I'd like to do is have it return one row per tip, with the count of up and down votes, sorted by the difference of up and down. By this I mean that if there are two tips, a and b:

a: 3 up, 2 down (diff: 1)
b: 4 up, 1 down (diff: 3)

Then b would come first and a would come second, since b has a higher overall score.

I really don't care how complicated the query is, I just can't think of how I could do it in PHP.

Oh, and in addition to the overall rating I also need the individual up and down values. Using the above example for b, I'd like this result:

Expand|Select|Wrap|Line Numbers
  1. up | down | rating | tip_id | tip_date | user_id | tip_text
  2. 4  | 1    | 3      | 1      | 12345678 | 1       | "This is some text"
Can someone suggest me a way to do this? It's sorta the defining feature of my project, letting people vote on submitted tips and having higher-rated tips appear higher in the list.

Edit: As a side note here's the query I'm currently using to get the up / down counts for a specified tip:

Expand|Select|Wrap|Line Numbers
  1. SELECT
  2.   count(CASE WHEN vote = 1 THEN 'up' ELSE NULL END) AS up,
  3.   count(CASE WHEN vote = 0 THEN 'down' ELSE NULL END) AS down
  4.   FROM votes
  5.   WHERE tip_id = 1
I tried modifying it a little to try to include everything from the tips table as well, like this:

Expand|Select|Wrap|Line Numbers
  1. SELECT
  2.   count(CASE WHEN V.vote = 1 THEN 'up' ELSE NULL END) AS up,
  3.   count(CASE WHEN V.vote = 0 THEN 'down' ELSE NULL END) AS down,
  4.   T.*
  5.   FROM votes V, tips T
  6.   WHERE V.tip_id = 1
And it sorta worked, but for some reason the up and down count are double what they should be, so if a tip has 2 up votes, the number displayed is actually 4. No clue why, any ideas?

Edit 2: I should also note that if a challenge ID has more than one tip associated with it, then the query should return each tip in it's own row. A problem I'm having right now is that I'm getting a total count of up and down votes for all tips for the specified challenge, in a single row. The problem with the two queries I posted in the first edit is that they perform the query based on the value of the tip_id, which means I can only get 1 tip at a time. I need to return all tips for the challenge, so I need to perform the query using the value of challenge_id.
Mar 22 '11 #1
1 1908
HaLo2FrEeEk
404 Contributor
I just figured I'd post an answer here. Since no help was forthcoming, I asked the same question on StackOverflow and got the answer I was looking for. MY final query is a lot simpler than I expected it to be, and I'm really happy to be learning about [LEFT/INNER/OUTER] JOIN, it's so useful! Here's the query I'll be using:

Expand|Select|Wrap|Line Numbers
  1. SELECT
  2.   COUNT(CASE WHEN V.vote = 1 THEN 'up' ELSE NULL END) AS up,
  3.   COUNT(CASE WHEN V.vote = 0 THEN 'down' ELSE NULL END) AS down,
  4.   (COUNT(CASE WHEN V.vote = 1 THEN 'up' ELSE NULL END)-COUNT(CASE WHEN V.vote = 0 THEN 'down' ELSE NULL END)) AS rating,
  5.   T.*
  6.   FROM tips T
  7.   LEFT JOIN votes V
  8.   ON T.tip_id = V.tip_id
  9.   WHERE T.challenge_id = 10
  10.   GROUP BY T.tip_id
  11.   ORDER BY rating DESC
All I have to provide to it is the challenge_id, it'll do all the work from there. The best part is, because of the LEFT JOIN, tips that don't have any votes will still appear in the result, with their up, down, and rating fields set to 0.

I posted this so that someone in the future with a similar problem can have the resource.
Mar 24 '11 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

9
19595
by: Deja User | last post by:
This is probably very simple but I can't figure out a way to update one table from another table. Here is an example: ------ TABLEA ------ first_name last_name
1
31097
by: Mike9900 | last post by:
What is the best way to copy DataRow from one table to another table, without copying its structure, which means copying only its data. -- Mike
1
1722
by: thengfen | last post by:
Hi! Im having a problem in transfering a set of records from a table to another table. The scenario is when i select combo box (Course taken such as diploma in IT), then the process will...
1
2227
by: satishkhachane | last post by:
-------------------------------------------------------------------------------- I want to select one or more columns from one typed datatable and add it to another table . how I can do this ? ...
1
1982
by: Sakakini | last post by:
How can I append last entry from one table to another table???
4
4831
by: xoozlez | last post by:
Hi there, How do I insert new records from a dbo table to another table? This is what I have: 1 dbo_company 1 Member (table) I made a query in dbo_company with the criteria I only want to...
2
2227
by: tomash | last post by:
Hi! I ve got two tables in Access 2007. I want to update a field of DataTable from another table, DataSumTable when two of their fields equals. ( the fields : Name and Period) I tried this...
5
5308
by: eBob.com | last post by:
I am so frustrated. I've been working all weekend on what I thought would be a simple script. I never find it easy to look at someone else's code, but if someone can help with this I would be...
3
2573
by: anil2083 | last post by:
How to migrate the comma separated values from one table to another table? suppose we have table i.e XYZ and we have comma separated values in few columns i.e( column_name and values are...
0
7193
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7067
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7264
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7316
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6975
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5562
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4666
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3160
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
371
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.