473,396 Members | 1,998 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

MySQL Query Help!!

Hopefully someone can help me here.

I have a personnel database with numerous tables, although only a few are
required for this particular query. I will name them as follows:

master - holds personal details about an employee,with the "staffno" field
as primary key;

psnexams - details number and type of exams a person has passed. Linked to
the master table using staffno field.
The fields in this table are:
- staffno - where the master table can be joined;
- psnexamid - primary key;
- cdeexamid - an integer, betwen 1 and 5, representing exam type;
- examresult - where pass = 1, fail = 0

There are 5 types of exams, numbered 1 to 5. These exams are split into 2
categories, namely 1, 2, 3 an 4, 5. In order to achieve an overall pass, an
employee must have passed EITHER the first 3 OR the last 2 (i.e. an employee
should have registered a pass in cdeexamid 1 AND 2 AND 3..... OR 4 AND 5 to
qualify for an overall pass)

I am attempting to write single query that will capture both types of
passes. I have managed to capture a list, but using two queries, shown
below:

SELECT DISTINCT master.payno AS "Pay Number",
master.forename AS "Forename",
master.surname AS "Surname",
count (psnexams.examresult) as "Passes"
from `master`
LEFT JOIN psnexams on master.staffno = psnexams.staffno
where master.inservice = 1
AND psnexams.cdeexamid in (1, 2, 3)
AND psnexams.examresult = 1
group by psnexams.staffno having Passes = 3

OR

SELECT DISTINCT master.payno AS "Pay Number",
master.forename AS "Forename",
master.surname AS "Surname",
count (psnexams.examresult) as "Passes"
from `master`
LEFT JOIN psnexams on master.staffno = psnexams.staffno
where master.inservice = 1
AND psnexams.cdeexamid in (4, 5)
AND psnexams.examresult = 1
group by psnexams.staffno having Passes = 2

What I am looking for is a query that will capture both lists in a single
uery.

Any help would be greatly appreciated.

Sparkybhoy

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.684 / Virus Database: 446 - Release Date: 13/05/2004
Jul 20 '05 #1
1 1411
Mark Hargreaves wrote:
What I am looking for is a query that will capture both lists in a single
query.


How about this:

SELECT DISTINCT M.payno AS "Pay Number",
M.forename AS "Forename", M.surname AS "Surname",
COUNT(P1.cdeexamid) + COUNT(P2.cdeexamid) AS Passes
FROM `master` M
INNER JOIN psnexams P1 ON (P1.staffno = M.staffno)
INNER JOIN psnexams P2 ON (P2.staffno = M.staffno)
WHERE M.inservice = 1
AND P1.cdeexamid IN (1,2,3) AND P1.examresult = 1
AND P2.cdeexamid IN (4,5) AND P2.examresult = 1
GROUP BY M.staffno
HAVING COUNT(P1.cdeexamid) = 3 OR COUNT(P2.cdeexamid) = 2;

Bill K.
Jul 20 '05 #2

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

Similar topics

6
by: Xenophobe | last post by:
I know this isn't a MySQL forum, but my question is related to a PHP project. I have two tables. table1 table2 "table1" contains 2 columns, ID and FirstName:
0
by: Frank | last post by:
This one has me pretty stumped at the moment. I've tried variations with MySQL IF() statements but can't quite get it right. How can I select all of the records of the table 'items' that are...
0
by: mlw | last post by:
Hi, im working on a pretty basic friends system for a website, so far it is all working but i have a Friends of Friends feature - which works to an extent but because of the way my queries are set...
1
by: emmah | last post by:
Dear Everyone, I have a small but hopefully simple problem. Please forgive my misuse of technical terminology but I am hoping someone can provide some advise. We have a database of nearly 1.6...
1
by: emmah | last post by:
Hi everyone, I know this will be simple but I just cant get it to work and I am being tested here by my line manager. Our database is named: lsql_Category and we have a table column named:...
2
by: Flic | last post by:
Hi, I have a basic db that I access with MySQL query browser. Everything seems fine to me but I am using this db as part of a php shopping basket and when I try to add an item I get: Notice:...
4
verbatim
by: verbatim | last post by:
i have 4 tables in a db, to allow users rate certain recipes. they might not rate all recipes at one time. they might even go back and rate a recipe more than one time. there will also be at least 1,...
1
by: pretzelboy | last post by:
Hi, I last wrote software 13years ago in the pascal, dbase, clipper days. I have recently built a Ubuntu Box and with C++ (and help from the web) setup a serial barcode reader program using Mysql...
4
by: ripefruit | last post by:
I want to insert a number into field #2 if a number in field #1 matches x Both fields are in the same table in the same db. field 1 = CatParent field 2 = CatParent2 What query do I run to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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
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...
0
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
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,...

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.