can anyone help me with this query? is it possible to do something like this?
I am very new to sql.
table1 ‘userJobId’ have 2 coloumn userid and jobgroupid,
table2 ‘tbl14userJob’ have a14userID and a14jobID as coloumn.
I need to add data to table1 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?
2
Answers
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):
You can achieve your goal using a combination of SQL and PHP logic. Since SQL doesn’t have direct "if" statements in the way programming languages do, you can handle the conditions in PHP before building your SQL query.
Here’s how you can do it:
a14userID
anda14jobID
fromtbl14userJob
.jobgroupid
based on the value ofa14jobID
.userJobId
accordingly.Here’s a sample implementation:
Explanation:
tbl14userJob
.jobgroupid
: Use PHP to set thejobgroupid
based on the value ofa14jobID
.Note: