473,498 Members | 1,936 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

SQL - identifying missing matches in two tables

Hi Everyone,

I'm trying to perform a query on some responses I have stored. I want to be
able to add up a score (using SUM() ) but first checking that all the
questions have been answered.

I've tried
SELECT *
FROM `questions`
LEFT JOIN `responses`
ON `questions`.`qid` = `responses`.`qid`
AND `responses`.`yearid` = 1 AND `responses`.`uid` = 1
WHERE `value` = NULL OR '-1'
ORDER BY value DESC

but it only returns all the rows even if value is '1' those question

a entry for a question is created with a `value` value of -1 when it is
accessed.

In short, I need to be able to identify those question which either don't
have an entry in `responses` for them or have an entry set to `-1`

RESPONSES
+-------+---------+
| Field | Type |
+-------+---------+
| uid | int(11) |
| yearid | int(11) |
| qid | int(11) |
| value | int(1) |
+-------+---------+

QUESTIONS
+----------+--------------+
| Field | Type |
+----------+--------------+
| qid | int(11) |
| qtext | varchar(100) |
+----------+--------------+

This is really bugging me. I can seem to make sense of it in my head, but
not able to translate it to SQL
Hope someone can help,

Cheers,
Greg
Jul 19 '05 #1
2 1824
"The Wanderer" <no***************@spamdial.pipex.com> wrote in message
news:bg**********@newsg2.svr.pol.co.uk...
Hi Everyone,

I'm trying to perform a query on some responses I have stored. I want to be able to add up a score (using SUM() ) but first checking that all the
questions have been answered.

I've tried
SELECT *
FROM `questions`
LEFT JOIN `responses`
ON `questions`.`qid` = `responses`.`qid`
AND `responses`.`yearid` = 1 AND `responses`.`uid` = 1
WHERE `value` = NULL OR '-1'
ORDER BY value DESC

but it only returns all the rows even if value is '1' those question

a entry for a question is created with a `value` value of -1 when it is
accessed.

In short, I need to be able to identify those question which either don't
have an entry in `responses` for them or have an entry set to `-1`

RESPONSES
+-------+---------+
| Field | Type |
+-------+---------+
| uid | int(11) |
| yearid | int(11) |
| qid | int(11) |
| value | int(1) |
+-------+---------+

QUESTIONS
+----------+--------------+
| Field | Type |
+----------+--------------+
| qid | int(11) |
| qtext | varchar(100) |
+----------+--------------+

[snip]

I've come a little further with
SELECT *
FROM questions AS q
LEFT JOIN responses AS r1 ON q.qid = r1.qid
JOIN responses AS r2 ON r1.uid = 1 AND r1.uid = r2.uid AND r1.qid = r2.qid
AND r1.yearid = 1 AND r1.yearid = r2.masid
WHERE r1.value IS NULL OR r1.value = '-1'

but because of the join it only returns those values which have -1 in them,
not those with NULL.
I feel like I'm so close, maybe I just need one extra word or condition and
it'll all fall into place.

Greg

Jul 19 '05 #2
"The Wanderer" <no***************@spamdial.pipex.com> wrote in message
news:bg**********@newsg2.svr.pol.co.uk...
Hi Everyone,

I'm trying to perform a query on some responses I have stored. I want to be able to add up a score (using SUM() ) but first checking that all the
questions have been answered.

I've tried
SELECT *
FROM `questions`
LEFT JOIN `responses`
ON `questions`.`qid` = `responses`.`qid`
AND `responses`.`yearid` = 1 AND `responses`.`uid` = 1
WHERE `value` = NULL OR '-1'
ORDER BY value DESC

but it only returns all the rows even if value is '1' those question

a entry for a question is created with a `value` value of -1 when it is
accessed.

In short, I need to be able to identify those question which either don't
have an entry in `responses` for them or have an entry set to `-1`

RESPONSES
+-------+---------+
| Field | Type |
+-------+---------+
| uid | int(11) |
| yearid | int(11) |
| qid | int(11) |
| value | int(1) |
+-------+---------+

QUESTIONS
+----------+--------------+
| Field | Type |
+----------+--------------+
| qid | int(11) |
| qtext | varchar(100) |
+----------+--------------+

[snip]

I've come a little further with
SELECT *
FROM questions AS q
LEFT JOIN responses AS r1 ON q.qid = r1.qid
JOIN responses AS r2 ON r1.uid = 1 AND r1.uid = r2.uid AND r1.qid = r2.qid
AND r1.yearid = 1 AND r1.yearid = r2.masid
WHERE r1.value IS NULL OR r1.value = '-1'

but because of the join it only returns those values which have -1 in them,
not those with NULL.
I feel like I'm so close, maybe I just need one extra word or condition and
it'll all fall into place.

Greg

Jul 19 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1528
by: Dan Bishop | last post by:
I installed Python on the HP 3000 at work today. The interpreter itself appears to be working fine, but "import math", "import datetime", etc. fail with "ImportError: No module named ". ...
0
688
by: Duncan Smith | last post by:
Hello, I'm not very experienced in SQL and I need some advice. I have a comma separarated values file containing around 20 million records and about 20 fields. There are many missing values...
3
1900
by: david_0 | last post by:
I have inherited support for a database with many used tables. There isn't any documentation on what is used or dead. I was hoping to run traces and capture the objectid but the property doesn't...
3
2533
by: wxbuff | last post by:
I have a report based on our product names that consists of two parts. Both insert data into a temporary table. 1. A single grouped set of results based on all products 2. Multiple tables based...
2
1987
by: gssstuff | last post by:
I have a piece of code I use to compare two identically structured tables. There are 15+ sets of tables I am comparing. I am looking to see what has changed between the "old" and "new" versions...
7
2450
by: bill.brennum | last post by:
Hi, Have a number of Access Databases that I inherited and want to zip a few of them. My concern is that other active Microsoft Applications may be linking to the database or its tables. Is...
3
2111
by: Christopher Weaver | last post by:
I want to set a value in a specific field in the current row of a DataSet. This seems like the most basic thing to do but I can't find the syntax for identifying the current row. IOW, I can do...
17
2986
by: Justin Emlay | last post by:
I'm hopping someone can help me out on a payroll project I need to implement. To start we are dealing with payroll periods. So we are dealing with an exact 10 days (Monday - Friday, 2 weeks). ...
2
2125
by: pb648174 | last post by:
I frequently have the problem where I have a list of items to delete in a temp table, such as ProjectId Description ------------- ---------------- 1 ...
1
4345
by: =?Utf-8?B?VGFz?= | last post by:
Hi, first of all, I'm using: Microsoft Visual Studio 2005 Version 8.0.50727.762 (SP.050727-7600) Microsoft .NET Framework Version 2.0.50727 Installed Edition: C# Express. I have added a...
0
7165
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
7203
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
6885
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
7379
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5462
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
3093
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
3081
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
656
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
290
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.