MySQL IF expression returns error - mysql

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

Related

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

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;
}
}

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

Why this CASE...END CASE clause in a trigger of MySQL caused an error prompt?

In a trigger of MySQL, I wrote such clause:
CASE v_index
WHEN 1 THEN:
UPDATE `device` SET `filter1_left` = `filter1_left` + 1 WHERE `id` = 100;
WHEN 2 THEN:
UPDATE `device` SET `filter2_left` = `filter2_left` + 1 WHERE `id` = 100;
WHEN 3 THEN:
UPDATE `device` SET `filter3_left` = `filter3_left` + 1 WHERE `id` = 100;
WHEN 4 THEN:
UPDATE `device` SET `filter4_left` = `filter4_left` + 1 WHERE `id` = 100;
WHEN 5 THEN:
UPDATE `device` SET `filter5_left` = `filter5_left` + 1 WHERE `id` = 100;
END CASE;
But I can't create this trigger successfully, receiving the following error prompt:
Error Code: 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 ': UPDATE device SET filter1_left = filter1_left + 1' at line 17
And by the way, I have three more questions:
I felt the above clause kind of verbose. Are there any methods to simplify it and achieve the same goal more elegantly?
Can I omit the colon following the 'THEN'?
Can I put the UPDATE... sentence in the same line of 'WHEN...THEN'?
Thanks in advance!
There is no colon : after THEN, so remove all those colons.
I felt the above clause kind of verbose. Are there any methods to simplify it and achieve the same goal more elegantly?
I started writing it, but Gordon Linoff beat me to it (as usual :-)), so see his answer.
Can I omit the colon following the 'THEN'?
You can't, you have to :-)
Can I put the UPDATE... sentence in the same line of 'WHEN...THEN'?
Yes. SQL doesn't care about whitespace in general, so a space or line break are all the same.
You could fix the data structure, so you have five rows instead of five columns. But you can also do this in one update:
UPDATE device
SET filter1_left = filter1_left + (CASE WHEN v_index = 1 THEN 1 ELSE 0 END),
filter2_left = filter2_left + (CASE WHEN v_index = 2 THEN 1 ELSE 0 END),
filter3_left = filter3_left + (CASE WHEN v_index = 3 THEN 1 ELSE 0 END),
filter4_left = filter4_left + (CASE WHEN v_index = 4 THEN 1 ELSE 0 END),
filter5_left = filter5_left + (CASE WHEN v_index = 5 THEN 1 ELSE 0 END)
WHERE `id` = 100 AND v_index IN (1, 2, 3, 4, 5);

UPDATE with logical AND operator

I tried running this sql
UPDATE table
SET read = 1
WHERE col1_id = 2
AND col3_id = 1;
and it return an error (error in your sql syntax)
#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 'read = 1 WHERE team_notification_id = 2 AND listener_id = 1' at line 1
But when I used thesame WHERE and AND :), in a SELECT statement no error was returned.
SELECT *
FROM read
WHERE col1_id = 2
AND col3_id = 1
please what did I do wrong, I am no seeing it.
After keyword UPDATE there should be a table name. If your table has actually name table (that's a bit strange) you should escape it to let MySQL know it's not a keyword "table" but actual table name.
The same thing is about read.
Both table and read are MySQL keywords (the full list is here http://dev.mysql.com/doc/refman/5.7/en/keywords.html) so if it's your actual table and column names then you should escape it.
Better to escape all table and column names to prevent issues like that.
Escaping in MySQL is done with backtick symbol ` so your query will looks like:
UPDATE `table`
SET `read` = 1
WHERE `col1_id` = 2
AND `col3_id` = 1;
UPDATE tablename
SET `field_name` = 1
WHERE col1_id = 2
AND col3_id = 1;
UPDATE table
SET [read] = 1
WHERE col1_id = 2
AND col3_id = 1;

Correct syntax to use in mysql 5.5.25 for not exists

Hi I'm trying to write a cursor in powerbuilder 12 to fetch some records . Here this is my query.
I'm trying to fetch some records from the trninvhdr table which are not in the second table.
SELECT
INV_DATE,
INV_NO,
INV_TYPE,
CUR_CODE,
EXCH_RATE,
usd_rate,
CR_TERM,
DUE_DATE,
bl_date,
TOT_AMT
FROM trninvhdr
WHERE
COMP_CODE ='NFL1' AND
CUST_CODE = 'NLML' AND
INV_TYPE ='F' AND
INV_DATE <= '2016-03-25' AND
NOT EXISTS
(SELECT * FROM trninvoiceavailability WHERE trninvoiceavailability.COMP_CODE = trninvhdr.COMP_CODE
AND trninvoiceavailability.INV_TYPE = trninvhdr.INV_TYPE AND trninvoiceavailability.INV_NO = trninvhdr.INV_NO);
Here how I use it in the program.
DECLARE lc_retrieve CURSOR FOR
SELECT
trninvhdr.INV_DATE,
trninvhdr.INV_NO,
trninvhdr.INV_TYPE,
trninvhdr.CUR_CODE,
trninvhdr.EXCH_RATE,
trninvhdr.usd_rate,
trninvhdr.CR_TERM,
trninvhdr.DUE_DATE,
trninvhdr.bl_date,
trninvhdr.TOT_AMT
FROM trninvhdr
WHERE
COMP_CODE = :as_comp_code AND
CUST_CODE = :as_cust_code AND
INV_TYPE ='F' AND
INV_DATE <= :as_inv_date )AND
NOT EXISTS (SELECT * FROM trninvoiceavailability
WHERE trninvoiceavailability.COMP_CODE = trninvhdr.COMP_CODE
AND trninvoiceavailability.INV_TYPE = trninvhdr.INV_TYPE AND
trninvoiceavailability.INV_NO = trninvhdr.INV_NO);
open lc_retrieve ;
The query works fine in the mysql server but in the progra it gives me the following error .
Database c0038 SQLSTATE = 3700 MySQL ODBC 5.2 a Driver mysql id 5.5.25 You have an error in your syntax. check the manual that corresponds to your mysql version for the right syntax to use near NOT EXISTS (SELECT * FROM trninvoiceavailability.... at line 1.
What is the correct Syntax that I should use to work this query.
I can see a bracket in this code... where did it come from and where is it's friend?
INV_DATE <= :as_inv_date )AND
You need to remove ) from your query as per below-
INV_DATE <= :as_inv_date )AND
sholuld be INV_DATE <= :as_inv_date AND