Update the number of rows based on multi where conditions across tables - mysql

I have two tables: [Invoices], which has a field on Vendor ID "i_v_id," and [Inventory_adjustment], which has columns Debit, Credit, and flag value which is set to 0 by default.
In this situation, I need to update every row in the inventory_adjustment database that fits the WHERE requirements.
I tried several methods to update the rows, but they all resulted in error warnings.
Error Number: 1064 You have an error in your SQL syntax; check the
manual that corresponds to your MySQL server version for the right
syntax to use near '.inad_invoice_id SET inad_adjusted_flag = 1
WHERE IA.inad_date >= '2022-12' at line 1 UPDATE
inventory_adjustment IA JOIN invoices I ON I.i_id =
IA.inad_invoice_id SET inad_adjusted_flag = 1 WHERE
IA.inad_date >= '2022-12-25' AND IA.inad_date <= '2023-01-03'
AND I.i_v_id = '3'
Model
function Adjustment()
{
$data=array(
'IA.inad_adjusted_flag' => "1"
);
$datefrom = $this->input->post('datefrom');
$dateto = $this->input->post('dateto');
$this->db->where('IA.inad_date >=', $datefrom);
$this->db->where('IA.inad_date <=', $dateto);
$this->db->where('I.i_v_id', $this->input->post('vendor_id'));
$this->db->update('inventory_adjustment IA JOIN invoices I ON I.i_id = IA.inad_invoice_id',$data);
if ($this->db->affected_rows() > 0) {
return true;
} else {
return false;
}
}

Related

Why is MySQL rejecting the following query?

UPDATE table_one
SET user_accessible = 1
WHERE volume == 2
AND lesson_order == '04'
LIMIT 1
It's giving the following error:
You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '= '2' AND lesson_order == '04') LIMIT 1' at line 1
The query looks correct to me :(
You made a mistake on your comparison. Use single = not ==
UPDATE table_one
SET user_accessible = 1
WHERE volume = 2
AND lesson_order = '04'
LIMIT 1

How to implement "SHOW COLUMN FROM (SELECT Result) WHERE Column_name = 'column_name'"?

Good Evning My Brothers/Sisters.
I want to check whether a column is exist from select result.
I have tried some effort like below but instead it gives me an error.
SHOW COLUMNS
FROM (SELECT
hasil.skpd, hasil.kode_skpd, SUM(hasil.sudah_isi) AS sudah_isi, SUM(hasil.belum_isi) AS belum_isi
FROM
(
SELECT
a.id_pegawai, c.skpd, c.kode_skpd, a.id_satuan_organisasi, a.nama_pegawai, a.nip,
CASE
WHEN i.id_pegawai IS NULL THEN 0
ELSE 1
END AS sudah_isi,
CASE
WHEN i.id_pegawai IS NULL THEN 1
ELSE 0
END AS belum_isi
FROM
tbl_pegawai a
LEFT JOIN ref_skpd c ON a.id_satuan_organisasi = c.id_skpd
LEFT JOIN tbl_bapertarum i ON (a.id_pegawai = i.id_pegawai AND YEAR(i.tgl_lapor)='2015')
GROUP BY
a.id_pegawai
) hasil
WHERE
1 AND hasil.kode_skpd LIKE 'Badan Kepegawaian Daerah%'
GROUP BY
hasil.id_satuan_organisasi
ORDER BY
hasil.kode_skpd) WHERE Field = "kode_skpd"
This is the full error result:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '(SELECT
hasil.skpd, hasil.kode_skpd, SUM(hasil.sudah_isi) AS s' at line 2
How do i to find the existence of a column or columns from SELECT result ?
Thanks in advance Brothers/Sisters.

Mysql case statement not returning appropriate message after case evaluation

I am selecting engineStatus field from car table, the query looks like so :-
select engineStatus from car LIMIT 1
car table column can have values 1 or 0.
Assuming engineStatus is set to 1, the above query will return 1.
I want to return ON if engineStatus = 1 or return OFF when engineStatus = 0
I've tried below query, but SQL throws an Error
SELECT ( case engineStatus = 1 then SET engineStatus = 'ON' else SET status = 'OFF') FROM `car` WHERE 1
Mysql Says
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'then SET engineStatus = 'ON' else SET engineStatus = 'OFF') FROM `car` WHERE 1 LI' at line 1
How can i achieve this in mysql ?
You miss WHEN and there's no need SET here
The proper syntax is :
SELECT
( CASE
WHEN engineStatus = 1 THEN 'ON'
ELSE 'OFF'
END
) AS status
FROM `car`
WHERE 1
It should be:
SELECT ( case WHEN engineStatus = 1 then 'ON' else
'OFF' END) FROM `car`;

MySQL IF expression returns error

SELECT last_played INTO #lp FROM sr WHERE creator_id = 1 AND playing = 1 LIMIT 1;
SELECT stream_id INTO #sid FROM account_stream WHERE owner = 1 AND enabled = 1 LIMIT 1;
UPDATE sr SET last_played = 1412259166 WHERE creator_id = 1 AND playing = 1 LIMIT 1;
IF #lp != NULL THEN UPDATE streams SET duration = (duration + (1412259166 - #lp)) WHERE id = #sid LIMIT 1; END IF;
What I am trying to do: when #lp returns something (in other words, when playing is 1 in the first query), then execute the last update streams query.
I get this error in phpmyadmin tho:
1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'IF #lp != 0 THEN UPDATE streams SET duration = (duration + (1412259166 - #lp)) W' at line 1
I've never worked with mysql if conditions before, so, can anyone please help me?
UPDATE streams SET duration = (duration + (1412259166 - #lp))
WHERE #lp IS NOT NULL AND id = #sid

Mysql script update with if statement and case

Why Can't I use or why does it encounters an error. I'm trying to use a script as such as the one below. I'm a newbie when it comes to mysql and I tried creating scripts like this but I get the errr like the below
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '1 THEN dogs.dogcount = dogcount - dogdetails.total, dogdetails.statu' at line 4
Here's my script
UPDATE dogs
LEFT JOIN dogdetails ON dogs.username = dogdetails.username
SET dogs.dogcount =
CASE WHEN dogdetails.dogcount IS NOT 1 THEN dogs.dogcount = dogcount - dogdetails.total, dogdetails.status = 'Checked'
WHEN dogdetails.dogcount = 1 THEN dogs.pto = pto - dogdetails.total, dogdetails.status = 'Checked'
WHERE dogdetails.id=4
Is there somethng I'm missing or overlooked?
remember that you have to use the keyword 'end' after your case statements and to always return a value. The IS NOT keyword is also incorrect when comparing numbers. Here's a version of your query that should work:
UPDATE dogs
LEFT JOIN dogdetails ON dogs.username = dogdetails.username
SET dogs.dogcount = CASE WHEN dogdetails.dogcount != 1 THEN dogs.dogcount = dogcount-dogdetails.total else dogs.dogcount end,
dogs.pto= case WHEN dogdetails.dogcount = 1 THEN pto - dogdetails.total else pto end,
dogdetails.status = 'Checked'
WHERE dogdetails.id=4