How can I make my where statement work here? - mysql

I have this update query and it works when my where has 2 conditions but when I added 3 more it doesn't work. Can anyone look at it and tell me whats wrong. Thank you.
Heres my query:
$sSQLupdate = "UPDATE settings, holidaytype SET
settings.companyName = '$companyname',
settings.companyAddress = '$companyaddress',
settings.companyContact = '$companycontact',
settings.excessot1 = '$excessot1',
settings.multiplierOT1 = '$multiplier1',
settings.excessot2 = '$excessot2',
settings.multiplierOT2 = '$multiplier2',
settings.ndRangeFrom = '$ND_in',
settings.ndRangeTo = '$ND_out',
settings.multiplierNd = '$NDmul',
holidaytype.multiplier = '$regularmul', *(mustbe in holidaytype.id = '1')
holidaytype.multiplier = '$specialmul', *(mustbe in holidaytype.id = '2')
holidaytype.multiplier = '$doubleregmulmul', *(mustbe in holidaytype.id = '3')
holidaytype.multiplier = '$doublespecmul' *(mustbe in holidaytype.id = '4')
WHERE settings.id = 1 AND holidaytype.id = '1' AND holidaytype.id = '2' AND holidaytype.id = '3' AND holidaytype.id = '4'";

Try this:
WHERE settings.id = 1 AND holidaytype.id IN ('1','2','3','4')
To update the holidaytype.multiplier by condition depending on holidaytype.id I suggest to use CASE statement or seperate queries.

Because all the three conditions cannot be true simulataneouly ie,
WHERE settings.id = 1 AND holidaytype.id = '1' AND holidaytype.id = '2' AND holidaytype.id = '3' AND holidaytype.id = '4'";
if holidaytype.id = '1' then it cannot be '2' or '3' or '4'
You better need to use IN
WHERE settings.id = 1 AND holidaytype.id IN ('1','2','3','4')

you need to use OR instead of AND AND because they cant be true all at the same time , one of them must be true. try this:
WHERE settings.id = 1
AND (holidaytype.id = '1' OR holidaytype.id ='2' OR holidaytype.id ='3' OR holidaytype.id ='4')";
or use this with IN clause:
WHERE settings.id = 1 AND holidaytype.id IN ('1','2','3','4')";
Your Update have no meaning , update two tables with conditions of eachother. either update each table alone or use join the tables.

Use parenthesis to separate boolean statements:
WHERE settings.id = 1 AND (holidaytype.id = '1' OR holidaytype.id = '2' OR holidaytype.id = '3' OR holidaytype.id = '4');

Related

Update rows from multiple tables and

I am working on a project where I need to update 2 tables in a query.
I am doing this
UPDATE ward_beds a, ipd_patient_list b
SET
a.occupation_status = 'empty',
b.patient_status = 'Discharged'
WHERE
a.ward_id = b.ward_id AND b.patient_id = '4'
AND
b.appointment_id = '6' AND b.ward_id = '1'
so far it is working, now I want to update this
b.patient_status = 'Discharged'
on the row which is the last in all matching rows. I tried to put this
ORDER BY b.row_id DESC LIMIT 1
but it shows
#1221 - Incorrect usage of UPDATE and ORDER BY
error. How should I do it?
You can't use limit in this kind of update
but you could seach for the correct id using a subquery and join to the others tables
UPDATE ward_beds a
INNER JOIN (
select ward_id, max(row_id) last_id
from ipd_patient_list
group by ward_id
) t on t.ward_id = a.ward_id
INNER JOIN ipd_patient_list b ON a.ward_id = b.ward_id
AND b.patient_id = '4'
AND b.appointment_id = '6'
AND b.ward_id = '1'
AND b.row_id = t.last_id
SET a.occupation_status = 'empty',
b.patient_status = 'Discharged'

Multiple rows in a select with OR

I have this query and when I add a OR, return multiples rows, how I can fix this? I need another left join or Right join?
SELECT `images`.`id` as `id_image`, `images_lang`.`title` as `title`, `images_lang`.`autor` as `author`, `media_lang`.`file`, `category_lang`.`title` as `category`
FROM `images`
JOIN `images_lang` ON `images_lang`.`id_images` = `images`.`id`
JOIN `category_lang` ON `images`.`id_category` = `category_lang`.`id_category`
JOIN `media` ON `images`.`id` = `media`.`id_item`
JOIN `media_lang` ON `media`.`id` = `media_lang`.`id_media`
JOIN `relation` ON `relation`.`from_id` = `images`.`id`
JOIN `tag` ON `tag`.`id` = `relation`.`to_id`
JOIN `tag_lang` ON `tag`.`id` = `tag_lang`.`id_lang`
WHERE `media`.`table` = 'images'
AND `media_lang`.`id_lang` = '1'
AND `images_lang`.`id_lang` = '1'
AND `category_lang`.`id_lang` = '1'
AND `images`.`active` = 1
AND `relation`.`type` = 2
AND `tag_lang`.`id_lang` = '1'
AND `tag_lang`.`slug` = 'pa-am-oli'
OR `tag_lang`.`slug` = 'playa'
LIMIT 9
and the code in codeigniter, but I donĀ“t how add the or after the and ($keywords is an array for filter the elements)
$this->db->select('images.id as id_image, images_lang.title as title, images_lang.autor as author, media_lang.file, category_lang.title as category');
$this->db->from($this->table);
$this->db->join($this->table.'_lang',$this->table.'_lang.id_images = '.$this->table.'.id');
$this->db->join('category_lang',$this->table.'.id_category = category_lang.id_category');
$this->db->join('media',$this->table.'.id = media.id_item');
$this->db->join('media_lang','media.id = media_lang.id_media');
$this->db->where('media.table',$this->table);
$this->db->where('media_lang.id_lang',$id_lang);
$this->db->where($this->table.'_lang.id_lang', $id_lang);
$this->db->where('category_lang.id_lang', $id_lang);
$this->db->where('images.active', 1);
if($category){
$this->db->where('category_lang.title', $category);
}
if($keywords){
$this->db->join('relation', 'relation.from_id = '.$this->table.'.id');
$this->db->join('tag', 'tag.id = relation.to_id');
$this->db->join('tag_lang', 'tag.id = tag_lang.id_lang');
$this->db->where('relation.type', _IMAGES_2_TAGS_);
$this->db->where('tag_lang.id_lang', $id_lang);
foreach($keywords as $tag){
$this->db->or_where('tag_lang.slug', $tag);
}
}
if($from || $limit)
{
$this->db->limit((int)$limit, (int)$from);
}
$query = $this->db->get();
return $query->result_array();
I solved the query adding this (and a Select Distinct)
AND (`tag_lang`.`slug` = 'playa'
OR `tag_lang`.`slug` = 'pa-am-oli')
How I can add this in codeigniter?
I don't get what you need exactly, but if you want have just one line as a return by filtering using OR
In your case you should do the following:
WHERE (`media`.`table` = 'images' OR `tag_lang`.`slug` = 'playa')
AND (`media_lang`.`id_lang` = '1' OR `tag_lang`.`slug` = 'playa')
and so on, I hope that helps you and good luck!
Astute use of parentheses would solve your problem, but this is easier to read...
SELECT i.id id_image
, il.title
, il.autor author
, ml.file
, cl.title category
FROM images i
JOIN images_lang il
ON il.id_images = i.id
JOIN category_lang cl
ON cl.id_category = i.id_category
JOIN media m
ON m.id_item = i.id
JOIN media_lang ml
ON ml.id_media = m.id
JOIN relation r
ON r.from_id = i.id
JOIN tag t
ON t.id = r.to_id
JOIN tag_lang tl
ON tl.id_lang = t.id
WHERE m.table = 'images'
AND ml.id_lang = 1
AND il.id_lang = 1
AND cl.id_lang = 1
AND i.active = 1
AND r.type = 2
AND tl.id_lang = 1
AND tl.slug IN('pa-am-oli','playa')
LIMIT 9
... however, note that LIMIT without ORDER BY is fairly meaningless.

Using If Else in Mysql

SELECT process.process_name, com_jobcard.job_card_num,
IF process.UOM = '1' THEN SET (com_jobcard.dept_qty) AS Total_qty
ELSEIF process.UOM = '2' THEN SET (com_jobcard.total_stones) AS Total_qty
FROM timer_completed
INNER JOIN PROCESS ON process.id = timer_completed.process_id
INNER JOIN com_jobcard ON timer_completed.job_card_id = com_jobcard.id
AND timer_completed.report_date = '2015-09-15'
Hi friends I would like to retrieve the value of department quantity When process.UOM = 1 else if process.UOM = '2' THEN SET (com_jobcard.total_stones) AS Total_qty , please help me with the code , thanks in advance
You can use case-when something as
SELECT
process.process_name,
com_jobcard.job_card_num,
case
when process.UOM = '1' then com_jobcard.dept_qty
when process.UOM = '2' then com_jobcard.total_stones
end as Total_qty
FROM timer_completed
INNER JOIN PROCESS ON process.id = timer_completed.process_id
INNER JOIN com_jobcard ON timer_completed.job_card_id = com_jobcard.id
AND timer_completed.report_date = '2015-09-15'
NOTE : Right now there is no else so its better to add an else if both condition fails
case
when process.UOM = '1' then com_jobcard.dept_qty
when process.UOM = '2' then com_jobcard.total_stones
else 'something you want'
end as Total_qty
IF(condition, true_value, false_value) can be used this way
SELECT process.process_name, com_jobcard.job_card_num,
IF(process.UOM = '1', com_jobcard.dept_qty, IF(process.UOM = '2', com_jobcard.total_stones, 0)) AS Total_qty
FROM timer_completed
INNER JOIN PROCESS ON process.id = timer_completed.process_id
INNER JOIN com_jobcard ON timer_completed.job_card_id = com_jobcard.id
AND timer_completed.report_date = '2015-09-15'

SELECT column/s based on boolean value

I have a left join select statement that I want to select specific columns based on the value of a variable that has either a '0' or a '1'. I thought I could implement this using CASE, but I don't seem to be having any luck.
If $variable contains a '0' I want my select statement to retrieve only users.user, table2.total1 and table2.total2, but if the $variable contains a '1' I want to select only users.user and table2.total.
$value = '1';
$conn->query(
"select users.user, table2.total, table2.total1, table2.total2
FROM users
LEFT JOIN table2 on users.user = table2.total AND $table2.date = CURDATE()
WHERE users.user = 'marketing' OR users.user = 'sales'
");
Does anyone have any ideas? Thanks.
You don't need to do this with sql, if in fact the query differ only by the select clause you could use a conditional and a variable:
$value = '1';
if($variable == '1') {
$select = "select users.user, table2.total1, table2.total2 ";
}
else {
$select = "select users.user, table2.total ";
}
$conn->query(
$select . "
FROM users
LEFT JOIN table2 on users.user = table2.total AND $table2.date = CURDATE()
WHERE users.user = 'marketing' OR users.user = 'sales'
");

Mysql UPDATE query with two tables

I wanted to run this query:
UPDATE up SET up.pts = uc.checkin_worth WHERE uc.email = up.email AND uc.company_id = up.company_id AND uc.email = 'test#gmail.com' AND uc.company_id = '4' AND uc.qrcode = 'j'
However, I am getting an error because I don't know how to combine two tables (uc and up) in a UPDATE query.
Can anyone help me solve this?
Thanks,
Just use the normal JOIN syntax:
UPDATE up JOIN uc ON uc.email = up.email AND uc.company_id = up.company_id
SET up.pts = uc.checkin_worth
WHERE uc.email = 'test#gmail.com' AND uc.company_id = '4' AND uc.qrcode = 'j'
You can also use the old comma syntax, which is more similar to your original query:
UPDATE uc, up
SET up.pts = uc.checkin_worth
WHERE uc.email = up.email
AND uc.company_id = up.company_id
AND uc.email = 'test#gmail.com'
AND uc.company_id = '4'
AND uc.qrcode = 'j'
Try with this:
UPDATE up, uc
SET up.pts = uc.checkin_worth,
WHERE uc.email = up.email AND uc.company_id = up.company_id AND uc.email = 'test#gmail.com' AND uc.company_id = '4' AND uc.qrcode = 'j'