Can anyone help me with this query? Is it possible to do something like this? I am very new to SQL.
- Table 1
userJobId
has 2 columns,userid
andjobgroupid
- Table 2
tbl14userJob
hasa14userID
anda14jobID
as columns
I need to add data to userJobId
where userid = a14userID
for jobgroupid = 1 if a14jobID = ‘CUPE12MO’, jobgroupid = 2 if a14jobID = ‘TEACHOCCL’, jobgroupid = 3 if a14jobID = ‘COOPSTUDENT’
This is the part where I have to add the query
$sql = "INSERT INTO userJobId (userid, jobgroupid) value ";
$sql .= "('$uid', ??? ),";
$db->exec($sql);
How to run an if statement in SQL?
T Harish is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
8
Do learn how to use bind parameters to prevent SQL injection attacks (and bugs).
You do want to use a subquery, something like this (untested):
insert into userJobId (userid, jobgroupid) values (?, (
select
case tbl14userJob.a14jobID
when 'CUPE12MO' then 1
when 'TEACHOCCL' then 2
when 'COOPSTUDENT' then 3
end
from tbl14userJob
where tbl14userJob.a14userID=userJobId.userid
))
3