How to use IF() and ELSE () statements in MYSQL - mysql

I want to DELETE data OR UPDATE data in MYSQL. such that if column message_deleted_by is found in a string IN() then the query should delete rows where the WHERE clause(filtering) is true ELSE the query should update message_table column message_deleted_by with some data ....ALL IN THE SAME QUERY
if this can be achieved please help.
I'v tried and tried but it output errors.
$token = mysqli_real_escape_string($dbc_conn,encode64(getsecreteToken($_POST["token"])));
$mid = mysqli_real_escape_string($dbc_conn,$_POST["mid"]);
$rid = mysqli_real_escape_string($dbc_conn,$_POST["rid"]);
$sid = mysqli_real_escape_string($dbc_conn,$_POST["sid"]);
$group = implode(",",array($rid,$sid));
$IsLoggIn = '2';
$SQL = "
IF(
SELECT message_deleted_by AS mdb FROM $message_tatable
WHERE (
m.sender_id='$IsLoggIn' AND m.recipient_id='$rid'
) AND
m.token='$token' AND m.id='$mid'
) mdb IN($group) THEN
DELETE m,mf
FROM $message_tatable m
LEFT JOIN $message_files_tb mf ON
m.token=mf.token
WHERE (
m.sender_id='$IsLoggIn' AND m.recipient_id='$rid'
) AND
m.token='$token' AND m.id='$mid';
ELSE
UPDATE $message_tatable SET message_deleted_by='$IsLoggIn'
WHERE (
m.sender_id='$IsLoggIn' AND m.recipient_id='$rid'
) AND
m.token='$token' AND m.id='$mid';
END IF;
";
//QUERY database
$query = mysqli_query($dbc_conn,$SQL);
die(mysqli_error($dbc_conn));

Try this,
UPDATE $message_tatable m
LEFT JOIN $message_files_tb mf
ON m.token=mf.token
SET m=NULL,
mf=NULL
WHERE m OR mf=SELECT message_deleted_by FROM $message_tatable IN($group)
AND m.sender_id='$IsLoggIn' AND m.recipient_id='$rid'
AND m.token='$token' AND m.id='$mid'

Related

Updating the last row MYSQL

I want to update the last row in a mysql table
UPDATE logend
SET endsecs = endsecs+'$moretime'
WHERE id = (SELECT id FROM logend ORDER BY id DESC LIMIT 1)
But it doesn't work, because of this error:
ERROR 1093 (HY000): You can't specify target table 'logend' for update in FROM clause
In MySql you can't use the updated table in a subquery the way you do it.
You would get the error:
You can't specify target table 'logend' for update in FROM clause
What you can do is an UPDATE with ORDER BY and LIMIT:
$sql = "UPDATE logend SET endsecs=endsecs+'$moretime' ORDER BY id DESC LIMIT 1";
Can't you just get the max(id) and update that?
$sql = "UPDATE logend SET endsecs=endsecs+'$moretime' WHERE id = (
SELECT id FROM (
SELECT MAX(id) FROM logend) AS id
)";
Here's another solution: a self-join, to find the row for which no other row has a greater id.
Also, you should really not interpolate POST inputs directly into your SQL statement, because that exposes you to SQL injection problems. Use a query parameter instead.
$moretime = $_POST['moretime'];
$sql = "
UPDATE logend AS l1
LEFT OUTER JOIN logend AS l2 ON l1.id < l2.id
SET l1.endsecs = l1.endsecs + ?
WHERE l2.id IS NULL";
$stmt = $mysqli->prepare($sql);
if (!$stmt) {
trigger_error($mysqli->error);
die($mysqli->error);
}
$stmt->bind_param("s", $moretime);
$ok = $stmt->execute();
if (!$ok) {
trigger_error($stmt->error);
die($stmt->error);
}

How to get records that match value or exist in another table?

I am trying to figure out how to get all tasks in this case that two of the fields equal a certain value or they exist in the other table?
Here is the query:
SELECT TASKS.task_id, TASKS.task_title, TASKS.task_description, TASKS.task_assigned_name, TASKS.task_assigned_phone_number, TASKS.task_due_date_time, TASKS.task_category
FROM TASKS
WHERE TASKS.task_complete = 1 AND
(TASKS.task_creator_id = ? OR
TASKS.task_assigned_user_id = ? OR
WHERE EXISTS (SELECT WATCHERS.task_id
FROM WATCHERS
WHERE WATCHERS.task_id = TASK.task_id AND
WATCHERS.watcher_user_id = ?
)
);
This is not returning anything even though I am expecting a result from my db.
You seem to have an error in your syntax. You have too many WHEREs:
SELECT t.task_id, t.task_title, t.task_description, t.task_assigned_name, t.task_assigned_phone_number, t.task_due_date_time, t.task_category
FROM TASKS t
WHERE t.task_complete = 1 AND
(t.task_creator_id = ? OR
t.task_assigned_user_id = ? OR
EXISTS (SELECT 1 -- the return value is immaterial
FROM WATCHERS w
WHERE w.task_id = t.task_id AND
w.watcher_user_id = ?
)
);
The WHERE before EXISTS is not appropriate.
Your query should be returning an error. Be sure to check for errors!
Have you tried using a join?
SELECT TASKS.task_id,
TASKS.task_title,
TASKS.task_description,
TASKS.task_assigned_name,
TASKS.task_assigned_phone_number,
TASKS.task_due_date_time,
TASKS.task_category
FROM TASKS
JOIN WATCHERS on WATCHERS.task_id = TASK.task_id
WHERE TASKS.task_complete = 1 AND
(TASKS.task_creator_id = ? OR
TASKS.task_assigned_user_id = ? OR
WATCHERS.watcher_user_id = ?);
I'm not sure if that's the logic you are looking for.
besides the extra where in your query, looks like you may have an extra closed parenthesis.
This sql matches the result set you posted in your duplicate post that has sample data and result:
SELECT t.task_id, t.task_complete, t.task_creator_id, t.task_assigned_user_Id
FROM tasks t
WHERE t.task_complete = 1 AND
(
t.task_creator_id = 8
OR t.task_assigned_user_id = 8
OR EXISTS
(
SELECT w.task_id
FROM watchers w
WHERE w.task_id = t.task_id
AND w.watcher_user_id = 8
)
)

Need records from one table if value matches or if value exists in another table [duplicate]

I am trying to figure out how to get all tasks in this case that two of the fields equal a certain value or they exist in the other table?
Here is the query:
SELECT TASKS.task_id, TASKS.task_title, TASKS.task_description, TASKS.task_assigned_name, TASKS.task_assigned_phone_number, TASKS.task_due_date_time, TASKS.task_category
FROM TASKS
WHERE TASKS.task_complete = 1 AND
(TASKS.task_creator_id = ? OR
TASKS.task_assigned_user_id = ? OR
WHERE EXISTS (SELECT WATCHERS.task_id
FROM WATCHERS
WHERE WATCHERS.task_id = TASK.task_id AND
WATCHERS.watcher_user_id = ?
)
);
This is not returning anything even though I am expecting a result from my db.
You seem to have an error in your syntax. You have too many WHEREs:
SELECT t.task_id, t.task_title, t.task_description, t.task_assigned_name, t.task_assigned_phone_number, t.task_due_date_time, t.task_category
FROM TASKS t
WHERE t.task_complete = 1 AND
(t.task_creator_id = ? OR
t.task_assigned_user_id = ? OR
EXISTS (SELECT 1 -- the return value is immaterial
FROM WATCHERS w
WHERE w.task_id = t.task_id AND
w.watcher_user_id = ?
)
);
The WHERE before EXISTS is not appropriate.
Your query should be returning an error. Be sure to check for errors!
Have you tried using a join?
SELECT TASKS.task_id,
TASKS.task_title,
TASKS.task_description,
TASKS.task_assigned_name,
TASKS.task_assigned_phone_number,
TASKS.task_due_date_time,
TASKS.task_category
FROM TASKS
JOIN WATCHERS on WATCHERS.task_id = TASK.task_id
WHERE TASKS.task_complete = 1 AND
(TASKS.task_creator_id = ? OR
TASKS.task_assigned_user_id = ? OR
WATCHERS.watcher_user_id = ?);
I'm not sure if that's the logic you are looking for.
besides the extra where in your query, looks like you may have an extra closed parenthesis.
This sql matches the result set you posted in your duplicate post that has sample data and result:
SELECT t.task_id, t.task_complete, t.task_creator_id, t.task_assigned_user_Id
FROM tasks t
WHERE t.task_complete = 1 AND
(
t.task_creator_id = 8
OR t.task_assigned_user_id = 8
OR EXISTS
(
SELECT w.task_id
FROM watchers w
WHERE w.task_id = t.task_id
AND w.watcher_user_id = 8
)
)

Need to speed up WHERE NOT EXISTS Query

I believe this is causing anywhere from a 5 minute to 20 minute delay depending on the number of records. I need to translate it into a LEFT JOIN but need some help getting it there.
qry_arr = array(':bill_type' => "INT");
$sql = "update ".$billing_table." c set c.bill_type = :bill_type";
$sql .= " WHERE NOT EXISTS (SELECT s.abbreviation FROM state s WHERE s.abbreviation = c.out_location)";
$sql .= " and c.out_location != 'UNKNOWN' and c.out_location != ''";
UPDATE $billing_table c
LEFT JOIN state s ON s.abbreviation = c.out_location
SET c.bill_type = :bill_type
WHERE s.abbreviation IS NULL
AND c.out_location NOT IN ('UNKNOWN', '')
This is essentially the same as the syntax for a SELECT for the rows that don't match. See Return row only if value doesn't exist. Just replace SELECT ... FROM with UPDATE, and insert the SET clause before WHERE.
Make sure you have indexes on out_location and abbreviation.

hibernate, mysql

i have the following hql query:
UPDATE TaskAssessment taskAssessment
SET taskAssessment.activeFlag = false
WHERE taskAssessment IN
(
SELECT taskAssessment2
FROM TaskAssessment taskAssessment2
Where taskAssessment2.activeFlag = true
AND taskAssessment2.patient.id
AND taskAssessment2.needsLevel.careNeed = :careNeed
)
but its giving me errors:
You can't specify target table 'TASK_ASSESSMENT' for update in FROM clause
could anyone help me to correct the query for mysql and hibernate. thanks in advance.
To resolve You can't specify target table 'TASK_ASSESSMENT' for update in FROM clause, rewrite the query to use JOIN instead of IN (in mysql you need to write something like this):
UPDATE TaskAssessment a
INNER JOIN TaskAssessment a2 ON (a2.id = a.id)
SET a.activeFlag = 0
WHERE a2.active_flag = 1 AND
a2.patient_id = :patient_id AND a2.needsLevel_careNeed = :careNeed