I am using a CTE inside a stored procedure, and it works ok, but the problem is when I try to add a conditional, I got the following message: You have an error in your sql .. Syntax to use near ‘IF. Answer You cannot use an IF statement in a query. It is meant to execute a block of
Tag: mysql
Joining INTO many-to-many table (From Primary Key Table)
This is how far i have gotten, though i don’t think it can be done with 1 SQL-statement, i just want to confirm whether or not it is possible to do this with ONLY 1 statement: SELECT * FROM users …
MySQL Workbench: “SELECT” is not valid at this position for this server version, expecting : ‘(‘, WITH error
I have the following SQL query which I have written in MySQL Workbench: SELECT t1.sample_id, t1.v_id, t1.s_type as type, t1.s_fusion, t1.e_fusion, CONCAT(t1.cpt1, “:”, t1.ppt1) as point1, …
How to use expression as LAG() second parameter on MySQL 8 (like MSSQL)?
I am trying to migrate this SQL SERVER query: dbfiddle link To MySQL 8 (same query): dbfiddle link Unfortunately I got this error: 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 ‘*(SELECT MIN(sales) FROM product_sales…
How long in years the artist was in the band for all artists who are or ever have been in bands
For the artists currently in bands, the value is null. How can I calculate how long they have been in the band if the value is null using mySQL? I was thinking of doing something like this, which only works for artists who have left a band. Answer I haven’t done SQL for a while so please bear with me.
Find IDs and names of students who took CS-101 and did not take CS-319
Can someone help with this homework problem: Find IDs and names of students who took CS-101 and did not take CS-319. These are the two tables that I can use for this assignment: I tried this but was unable to successfully exclude the students who who took “CS-319” Answer There are at least three s…
Using REPLACE with CHAR(160) is Returning Hexadecimal as Value
I am trying to get rid of   characters in MYSQL, but am getting weird behavior where using REPLACE is returning a hexadecimal string. The original value is some HTML stored in a field with the type BLOB: The SQL I am using is this: And after executing, this is what is left in the database: What is goi…
Is my site vulnerable to SQL injections if I get error 403?
I have a login form and just to test I tried to fill in “select * from accounts where username = test” and then I pressed enter to see what happens. I got redirected to this page: Should I be concerned about SQL injections? Or is this a normal response? Edit: the PHP code for this particular case.…
Uncaught mysqli_sql_exception: You have an error in your SQL syntax
I cant get my update button to work, my SQL syntax isnt targeting the datebase properly, this is the syntax im using $sql = (“UPDATE crud SET name=’$name’, last=’$last’, email=’$email’, birthday=’$…
MySql last record from group by item_id with order by date
My database table name is ledgers and fields are id, item_id, date, …other fields I Want the last record from (groupBy item_id order by date ASC). from each group. I tried below query Can you guys please help. Answer You can filter with a correlated subquery: For performance, consider an index on (item_…