sql - selecting, counting and Updating - sql

I'm wondering if this is correct...
I want to select all users from teams in a match and then add a win, loose or draw to his profile.
$short = $_POST['short'];
$opponent = $_POST['opponent'];
$oppuser = safe_query("SELECT userID FROM ".PREFIX."teams_members WHERE teamID='".$opponent."'");
$shortuser = safe_query("SELECT userID FROM ".PREFIX."teams_members WHERE teamID='".$short."'");
safe_query("UPDATE ".PREFIX."teams_members SET win=win+1 WHERE userID='".$oppuser."'");
safe_query("UPDATE ".PREFIX."teams_members SET lost=lost+1 WHERE userID='".$shortuser."'");
Something is not allowing updating the rows.

You don't need those selects. Your update is not working because the select statement is returning you multiple entries. You can update the entire team without selecting the users:
$short = $_POST['short'];
$opponent = $_POST['opponent'];
safe_query("UPDATE ".PREFIX."teams_members SET win=win+1 WHERE teamID='$opponent'");
safe_query("UPDATE ".PREFIX."teams_members SET lost=lost+1 WHERE teamID='$short'");

Related

Update table value based on presence in multiple possible arrays (SQL)

I have a members table with regions values in it. This table is first populated at sign up.
The states are sorted into an array for each region:
$region[1] = array("FL","GA","NC","SC","USVI","PR");
$region[2] = array("DC","KY","MD","OH","VA","WV");
$region[3] = array("DE","NJ","NY","PA");
$region[4] = array("CT","MA","ME","NH","RI","VT");
$region[5] = array("IA","IL","IN","MO","MI");
$region[6] = array("AL","AR","LA","MS","TN");
$region[7] = array("AZ","NM","OK","TX");
$region[8] = array("CO","KS","MN","MT","NE","ND","SD","UT","WI","WY");
I then do a recursive search to populate the table based on the region for that state.
$member_region = (recur_search($_SESSION['session_table_membersignup_mailing_state'], $region)) ? recur_search($_SESSION['session_table_membersignup_mailing_state'], $region) : 9;
I added additional regions, so that when new users sign up it will sort them based on the new arrays:
$region[1] = array("FL","GA","NC","SC","USVI","PR");
$region[2] = array("DC","KY","MD","OH","VA","WV");
$region[3] = array("DE","NJ","NY","PA");
$region[4] = array("CT","MA","ME","NH","RI","VT");
$region[5] = array("IA","IL","IN","MO","MI");
$region[6] = array("AL","AR","LA","MS","TN");
$region[7] = array("AZ","NM","OK","TX");
$region[8] = array("ND","NE","MI","MN","SD","WI");
$region[9] = array("AK","CA","NV","OR","WA","HI");
$region[10] = array("CO","ID","MT","UT","WY");
I'm running into issues on how to retroactively update the sql table for existing members using the new arrays.
Bsically: I need to loop through the Members table and update the regions value based on the mailing_state prescence in the new arrays.
I'm struggling with writing a working sql statement to do this. I figured since this is a one time update, I could just run it directly from the database.

Joining multiple different update queries into single query - PostgreSQL

Apologies if something similar has been asked before, currently I run 3 different update queries in order to get the desired result. The Queries are as follows,
UPDATE users SET enabled = false where username in (SELECT username
FROM users WHERE enabled = true AND lastaccess != 0 AND lastaccess
< (EXTRACT('epoch' FROM CURRENT_TIMESTAMP) - (86400*200))*1000 AND
username NOT LIKE ('admintest%'));
Once this query runs (disabling all users who haven't accessed the system in a certain period), then I run the following 2 Queries on the whole table,
update users set weeklypopupuse = 0;
update users set monthlypopupuse = 0;
These 2 queries then reset the weekly and monthly use to 0.
Now this works perfectly fine as per the requirements, however is there a more elegant way of writing all these 3 queries into a single query which gives the same result.
Any help or advice will be highly appreciated. Thanks in advance
update
after OP gave full structure, refactored to:
update users
set
weeklypopupuse = 0
, monthlypopupuse = 0
, enabled = case
when
enabled = true
AND lastaccess != 0
AND lastaccess < (EXTRACT('epoch' FROM CURRENT_TIMESTAMP) - (86400*200))*1000
AND username NOT LIKE ('admintest%')
then false
end

using criteria in an update query involving a join

I'm using MS Access
The SQL below updates the CurrNumTees field in the Parent tblContact records with the number of tblTorTee records that have an end date (which is not the ultimate effect I am aiming for, but I provide it as a starting point.
UPDATE tblContact
INNER JOIN tblTorTee ON tblContact.ContactId = tblTorTee.TorId
SET tblContact!CurNumTees = DCount("[tblTorTee.EndDate]",
"tbltortee","Torid = " & [ContactId]);
I need to update the CurrNumTees field with the number of records in tblTorTee that do not have an EndDate, in other words, that field is blank. I’ve tried using WHERE and HAVING and IS NULL in various combinations and locations, but without success. Could you help point me in the right direction?
The MS Access COUNT function does not count nulls, so I think you have to do this in two stages.
Firstly create a query like this:
SELECT TorId, IIF(ISNULL(EndDate),1,0) AS isN
FROM tblTorTee
WHERE EndDate IS NULL;
And save it as QryEndDateNull
Now you can run an Update Query like this:
UPDATE tblContact
SET tblContact.CurNumTees = DSUM("IsN","QryEndDateNull","TorId = " & [ContactID]);
Saving calculated data (data dependent on other data) is usually a bad design, especially aggregate data. Should just calculate when needed.
Did you try the IS NULL criteria within the DCount()?
UPDATE tblContact Set CurNumTees = DCount("*", "tblTorTee", "EndDate Is Null AND TorId = " & [ContactId]);

ORA-00904 Invalid Identifier - Update Statement With Two Tables

I'm working with PeopleSoft Campus Solutions, and we need to update about 22,000 rows of data. This is data between the tables of ACAD_PLAN_VW and ACAD_PROG. Students are listed on both, so their IDs match between the two.
Basically what we are trying to do is say that when the ID, academic career, student career number, effective sequence, and effective date match, AND where the academic plan (their degree, as stored on the ACAD_PLAN_VW) is a specific value, update the ACAD_PROG on the ACAD_PROG table to X value.
I tried to do some very interesting combinations of FROM statements, constantly getting errors. After some researching, I found out that SQLTools doesn't really like FROM statements within UPDATE statements, so I rewrote it to just make the connections manually. I'm assuming I'm doing this right, unless I need to reword it.
The statement I have is:
UPDATE PS_ACAD_PROG SET PS_ACAD_PROG.ACAD_PROG = 'UGDS'
WHERE PS_ACAD_PLAN_VW.EMPLID = PS_ACAD_PROG.EMPLID
AND PS_ACAD_PLAN_VW.ACAD_CAREER = PS_ACAD_PROG.ACAD_CAREER
AND PS_ACAD_PLAN_VW.STDNT_CAR_NBR = PS_ACAD_PROG.STDNT_CAR_NBR
AND PS_ACAD_PLAN_VW.EFFSEQ = PS_ACAD_PROG.EFFSEQ
AND PS_ACAD_PLAN_VW.EFFDT = PS_ACAD_PROG.EFFDT
AND PS_ACAD_PLAN_VW.ACAD_PLAN = 'DSTDS'
Theoretically, I would assume that this would update any student who has those connections. However, the error that I'm currently getting is as follows:
ORA-00904: "PS_ACAD_PLAN_VW"."ACAD_PLAN": invalid identifier
I have, as of yet, been unable to figure out the issue. I do have the correct access to view and update those fields, and the field does indeed exist.
Oracle doesn't know it should use the PS_ACAD_PLAN_VW table. Somehow you should reference it.
For example can you try this way?
UPDATE (
select
PS_ACAD_PROG.ACAD_PROG,
PS_ACAD_PLAN_VW.ACAD_PLAN
from
PS_ACAD_PROG,
PS_ACAD_PLAN_VW
where
PS_ACAD_PLAN_VW.EMPLID = PS_ACAD_PROG.EMPLID
AND PS_ACAD_PLAN_VW.ACAD_CAREER = PS_ACAD_PROG.ACAD_CAREER
AND PS_ACAD_PLAN_VW.STDNT_CAR_NBR = PS_ACAD_PROG.STDNT_CAR_NBR
AND PS_ACAD_PLAN_VW.EFFSEQ = PS_ACAD_PROG.EFFSEQ
AND PS_ACAD_PLAN_VW.EFFDT = PS_ACAD_PROG.EFFDT
)
SET
ACAD_PROG = 'UGDS'
WHERE
ACAD_PLAN = 'DSTDS'
Try to use the table which is not getting identified,by keeping in where clause so that you can use the select statement.That will help identifying the tables.
The following implementation should work:
UPDATE PS_ACAD_PROG
SET ACAD_PROG = 'UGDS'
WHERE ROWID IN(SELECT PROG.ROWID
FROM PS_ACAD_PROG PROG
, PS_ACAD_PLAN_VW PLAN
WHERE PLAN.EMPLID = PROG.EMPLID
AND PLAN.ACAD_CAREER = PROG.ACAD_CAREER
AND PLAN.STDNT_CAR_NBR = PROG.STDNT_CAR_NBR
AND PLAN.EFFSEQ = PROG.EFFSEQ
AND PLAN.EFFDT = PROG.EFFDT
AND PLAN.ACAD_PLAN = 'DSTDS');

Access 2010 SQL Update cmd

I'm having some difficulty with an Update statement.
UPDATE DownPeriod
SET
DownPeriod.EquipmentID = ? ?? ?? ? ?? ?? ? ?,
DownPeriod.DownDate = Forms!Edit!EditDownDateBox,
DownPeriod.DownTime = Forms!Edit!EditDownTimeBox,
DownPeriod.[UpDate] = Forms!Edit!EditUpDateBox,
DownPeriod.UpTime = Forms!Edit!EditUpTimeBox,
DownPeriod.isScheduled = Forms!Edit!EditSchedCheck,
DownPeriod.isUnscheduled = Forms!Edit!EditUnschedCheck,
DownPeriod.Comments = Forms!Edit!EditCommentsDropDown
WHERE DownPeriod.DownPeriodID =Forms!Edit!RecordHistorySubform.Form!DownPeriodID;
Where the question marks are is where im having difficulty, and am not sure what to put.
Everything else about the update will work if I remove that statement so I know im on the right track. The difference with the EquipmentID is that I'm getting this value based on an input for another table entry. To elaborate the user chooses an Equipment Title, which is another field in my Equip Table that will relate to a unique ID.
So far I have tried DLOOKUP("[EquipmentID]", "Equipment", "[Title] = Forms!Edit!EditEquipmentDropDown")
Select statment and
using inner join
I'm at a loss and need help plz!
Thank you!
You say that the user is selecting the title from a dropdown. The dropdown should have a row source on these lines:
SELECT ID, Title FROM Equipment
With ID as a hidden column. Your update should then be:
DownPeriod.EquipmentID = Forms!Edit!EditTitleDropDown
As an aside, I suspect you are doing a lot more work that you need to do to make an MS Access form work.
If it is a new unique id you need an INSERT. If it already exists you should include it as part of your WHERE clause.
WHERE DownPeriod.DownPeriodID =Forms!Edit!RecordHistorySubform.Form!DownPeriodID
AND DownPeriod.EquipmentID = Forms!Edit!EditEquipmentDropDown;