473,379 Members | 1,511 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,379 software developers and data experts.

Update query needed - on a "group by" query - Possible / How???

Hi there,

Been working on an evolving DB program for a while now. Suddenly I have come
across a situation where I need to update a table based on a group by query.

For example, I have a table called "students". I need to update a field
called "status" on this table for all members that have never attended a
class.

Class attendance is recorded by another table (which represents the many to
many relationship between a student and a class) called "studentclasslink"
which has fields, student_id, class_id as well as it's own primary key,
"studentclasslink_id".

Now how would I go about writing a query that updates the "status" field
only for students that have attended no classes?
At first I thought this was easy as I can display a count of how many
classes a student has attended by a query like so:

SELECT students.student_id, students.student_name, count(class_id) as
classcount
FROM students
LEFT JOIN `studentclasslink` ON students.student_id =
studentclasslink.student_id,
GROUP BY students.student_id

This will give me a list of each student and how many classes they have
attended. I want to update all the students who have not attended any
classes (therefore a classcount of 0).
Bear in mind that my host is running MySQL version 3.23.

Kind regards,

Dave


Jan 22 '06 #1
2 10067
"Dave Smithz" <da******@blueyonder.co.uk> wrote in message
news:B7*********************@fe2.news.blueyonder.c o.uk...
Now how would I go about writing a query that updates the "status" field
only for students that have attended no classes?
Bear in mind that my host is running MySQL version 3.23.
This version of MySQL means you cannot use subqueries, or multi-table UPDATE
statements.
SELECT students.student_id, students.student_name, count(class_id) as
classcount
FROM students
LEFT JOIN `studentclasslink` ON students.student_id =
studentclasslink.student_id,
GROUP BY students.student_id


That is very close. I'd do this:

SELECT S.student_id
FROM students AS S LEFT JOIN studentclasslink AS L ON S.student_id =
L.student_id
WHERE L.student_id IS NULL

Fetch the list of student_id values, and format the list as a string with
values comma-separated.
Then create an UPDATE statement including that string:

UPDATE students
SET status = ...value...
WHERE student_id IN ( ...comma-separated list of values from previous
query... )

Regards,
Bill K.
Jan 23 '06 #2

"Bill Karwin" <bi**@karwin.com> wrote in message
news:dr*********@enews4.newsguy.com...
Fetch the list of student_id values, and format the list as a string with
values comma-separated.
Then create an UPDATE statement including that string:

UPDATE students
SET status = ...value...
WHERE student_id IN ( ...comma-separated list of values from previous
query... )


OK, thanks for this. This is the method I went for in the end. Therefore
writing a PHP script to run a query to get all the ID's I needed to update
and then running a second query. I think I was trying to hard to do this in
a single SQL statement without subqueries.
I just assumed it would be possible to do conditional updates based on
values when grouping, but I guess it is more complicated then that.

Thanks for all the input.

Kind regards

Dave
Jan 24 '06 #3

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

Similar topics

3
by: Louis | last post by:
Is there a switch or a setting in Access so that a group by query doesn't return a field name SumOf(original field name)? Especially when you chain multiple queries together you'd get...
1
by: Chris Thunell | last post by:
I have a dataset / datatable in memory and i would like to run a query against that to create another in memory datatable or dataview. In this case I have a bunch of data... and i want to run a...
5
by: Dave Smithz | last post by:
Hi there, Been working on an evolving DB program for a while now. Suddenly I have come across a situation where I need to update a table based on a group by query. For example, I have a table...
1
by: richforsandy | last post by:
I am working with a two field table of 144,020 rows. The PERSONID field is the FK and the ADDRESSID is the PK. FK is a number type, PK is autonumber. PERSONID represents employees,...
0
by: laurenquantrell | last post by:
I have a table with 6 million rows. I'm trying to perform a group by query: SELECT COUNT(CityID) AS Cnt, Country FROM dbo.tblCitiesWorld WHERE (FC = N'p')
2
by: laurenquantrell | last post by:
I have a table with 6 million rows. I'm trying to perform a group by query that incudes a row count of the PK column: SELECT COUNT(DataID) AS Cnt, Column1Name FROM dbo.TableName WHERE...
0
by: Sebastien.LICHTHERTE | last post by:
Hello, I need to calculate the median and percentile of values in a group by query satisfying several criteria the user is asked to fill in when opening the query. A have a table called RX with...
3
by: =?Utf-8?B?VmFuZXNzYQ==?= | last post by:
Here is my loop and it runs fine: ---------------------------------------------------- sSQL = "SELECT * FROM STORE_ITEMS" Set DataRec = DB.execute(sSQL) if not DataRec.EOF then do while not...
2
by: dilippanda | last post by:
Hi, Please help me in the following query. SELECT a.bcast_id , a.bcast_file_nm ...
5
by: deepakkumars | last post by:
Hi All: I'm using Access 2003 on Windows XP. I'm creating a table where i'll be adding a new variable every week (based on the week number). This is a numeric variable. Once the variable is...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.