how to fix sql query error - mysql

I have this request
DELETE FROM T1 WHERE PERMISSION_ID = 'x' AND
0<(SELECT * FROM T2 WHERE "SUBSTR"(PID,1,1)=1) OR '1'='1'
but got
The SQL statement " DELETE FROM T1 WHERE PERMISSION_ID = 'x'
AND 0<(SELECT * FROM T2 WHERE SUBSTR(PID,1,1)=1) OR '1'='1'"
contains the syntax error[s]: - 1:101 - SQL syntax error: the token
"(" was not expected here
Please help, how I can fix it?
UPDATE
remove "SUBSTR" to SUBSTR, but get The SQL statement
DELETE FROM T1
WHERE PERMISSION_ID = 'x'
AND 0= (SELECT * FROM T2 WHERE SUBSTR(PID,1,1)=1) OR '1'='1'
contains the syntax error[s]: - 1:99 - SQL syntax error: the token "(" was not expected here
UPDATE 2
I change to
SELECT count(*) FROM T2 WHERE SUBSTRING("asdqweasd",1,1))
and get
- SQL syntax error: the token "," was not expected here
- expecting "from", found ','

You do not need Double quotes around SUBSTR. Also you can not use SELECT * . Use count(*)
DELETE FROM T1 WHERE PERMISSION_ID = 'x' AND
0<(SELECT count(*) FROM T2 WHERE SUBSTR(PID,1,1)=1) OR '1'='1'

Related

Select only the domain name from an email address

I'm trying to get the domain instead of the full e-mail address in my query for Domain_. In the statement below I select user_data. Email_addressbut that provides foo#bar.com instead of bar.com which isn't sufficient:
INSERT INTO user_orders
(`User_ID`, `Order_Number`, `Customer_ID`, `Email_address`, `Domain_`)
SELECT
user_data.`User_ID`, order_data.`Order_Number`, user_data.`Customer_ID`,
user_data.`Email_address`, user_data.`Domain_`
FROM user_data
INNER JOIN order_data ON order_data.User_ID = user_data.User_ID;
My understanding was SUBSTRING_INDEX(Email_address,'#',-1) as Domain_ would work so I tried:
INSERT INTO user_orders
(`User_ID`, `Order_Number`, `Customer_ID`, `Email_address`, `Domain_`)
SELECT
user_data.`User_ID`, order_data.`Order_Number`, user_data.`Customer_ID`, user_data.`Email_address`,
(SUBSTRING_INDEX(SUBSTR(user_data.`Email_address`, INSTR(user_data.`Email_address`, '#') + 1),'.',1)) as user_data.`Domain_`
FROM user_data
INNER JOIN order_data ON order_data.User_ID = user_data.User_ID;
but the query fails and provides a syntax error.
To help isolate it more for clarity this is what I'm doing differently:
(SUBSTRING_INDEX(SUBSTR(user_data.`Email_address`, INSTR(user_data.`Email_address`, '#') + 1),'.',1)) as user_data.`Domain_`
Any help would be appreciated as I'm stuck.
Error: INSERT INTO user_orders(User_ID, Order_Number, Customer_ID, Email_address, Domain_)
select user_data.User_ID, order_data.Order_Number, user_data.Customer_ID, user_data.Email_address, (SUBSTRING_INDEX(SUBSTR(user_data.Email_address, INSTR(user_data.Email_address, '#') + 1),'.',1)) as user_data.Domain_ FROM user_data
INNER JOIN order_data on order_data.User_ID = user_data.User_ID;
/* SQL Error (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 '.Domain_ FROM user_data
INNER JOIN order_data on order_data.User_ID = user...' at line 2 */```
If you execute for example
SELECT split_part(email, '#', 2) AS Domain FROM user_orders;
It will show everything after the '#' which is the domain you want.
All info
Now, using the query

Update records with JOIN command

I have the following query that works great and shows me where the tbl_staff.staff_id and tbl_lead.rlog_create_user_id values do not match.
SELECT
tbl_staff.staff_id,
tbl_staff.username,
tbl_lead.rlog_create_user_name,
tbl_lead.rlog_create_user_id
FROM
tbl_staff
JOIN tbl_lead ON tbl_staff.username = tbl_lead.rlog_create_user_name
AND tbl_staff.staff_id <> tbl_lead.rlog_create_user_id;
The query returns values like this where you can see the 1014 does not match the 1004.
1014 bubba bubba 1004
I want to update the value in the tbl_lead.rlog_create_user_id to the same value as is found in tbl_staff.staff_id.
I tried to insert a SET command but it's giving me a generic syntax error:
SELECT
tbl_staff.staff_id,
tbl_staff.username,
tbl_lead.rlog_create_user_name,
tbl_lead.rlog_create_user_id
FROM
tbl_staff
JOIN tbl_lead ON tbl_staff.username = tbl_lead.rlog_create_user_name
AND tbl_staff.staff_id <> tbl_lead.rlog_create_user_id
SET tbl_lead.rlog_create_user_id=tbl_staff.staff_id ;
The actual error is:
[Err] 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 'SET tbl_lead.rlog_create_user_id=tbl_staff.staff_id' at line
10
I tried to change the SELECT to and UPDATE command using this question but still could not get it working: How can I do an UPDATE statement with JOIN in SQL?
Try this
UPDATE tbl_lead
JOIN tbl_staff ON tbl_staff.username = tbl_lead.rlog_create_user_name
SET tbl_lead.rlog_create_user_id = tbl_staff.staff_id
WHERE tbl_staff.staff_id <> tbl_lead.rlog_create_user_id;
Have you tried like this ?
UPDATE tbl_staff, tbl_lead
SET tbl_lead.rlog_create_user_id = tbl_staff.staff_id
WHERE tbl_staff.username = tbl_lead.rlog_create_user_name

SQL nested queries and subqueries

i've been having an error with this query and i'm not sure how to fix it. this query is supposed to filter occupations stored in my database to match the volunteer's occupation. please help me fix my query. all the names in this query are correctly spelled, i double checked all the spellings before writing the query here.
the error says "#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 (SELECT count(*) FROM occupation_event WHERE event_id='8' AND occupationN' at line 1"
SELECT
*
FROM
volunteer_details
WHERE
user_id=73
AND
volunteer_occupation in (
SELECT
occupationName
FROM
occupation_event
WHERE
event_id=8
OR SELECT (
SELECT
count(*)
FROM
occupation_event
WHERE
event_id='8'
AND
occupationName = 'No Occupation Required') > 0 AS need
)
I think the error is the as need at the end. I would write this as:
SELECT vd.*
FROM volunteer_details vd
WHERE user_id = 73 AND
(vd.volunteer_occupation in (SELECT oe.occupationName FROM occupation_event oe WHERE oe event_id = 8) or
exists (select 1 from occupation_event oe where event_id = 8 and oe.occupationName = 'No Occupation Required')
);

sqldf gives error while using subquery

I'm trying to get moving average by weekday, for that I'm using sql query.
Dataframe is
and sqldf code:
ma_782 = sqldf("SELECT
t1.Id_indicator, t1.Hour,
(
select SUM(t2.Value) / COUNT(t2.Value)
FROM max_value_782 AS t2
WHERE
t1.Hour = t2.Hour and
weekdays.Date(t1.Date) = weekdays.Date(t2.Date)
and DATEDIFF(t1.Date, t2.Date) BETWEEN 1 AND 42
) AS 'MA_by_weekday'
FROM max_value_782 AS t1 ;")
This gives error
Error in rsqlite_send_query(conn#ptr, statement) : near "(": syntax error
while it works from simple select:
sqldf("select * from max_value_782")
Consider replacing the weekdays. method as this syntax assumes a table qualifier. Since by default sqldf uses the SQLite dialect, use strftime to compare weekdays. Also, single quotes are used for string literals and not to enclose table/field identifiers. SQLite can uses brackets, backticks, or double quotes, or none if reserved words/special characters are not used.
ma_782 = sqldf("SELECT t1.Id_indicator, t1.Hour,
(SELECT AVG(t2.Value)
FROM max_value_782 AS t2
WHERE t1.Hour = t2.Hour
AND strftime('%w', t1.Date) = strftime('%w', t2.Date)
AND (t2.Date - t2.Date) BETWEEN 1 AND 42
) AS MA_by_weekday
FROM max_value_782 AS t1;")

Why does ExtractValue() fail in an UPDATE and succeed in a SELECT?

When I execute this query...
UPDATE tbl a,
(SELECT id, EXTRACTVALUE(content, '//a[contains(text(), "View")]/#href') AS url FROM tbl) b
SET tbl.`url` = b.`url`
...I see this error:
Error Code: 1525
Incorrect XML value: 'parse error at line 57 pos 195: '</div>' unexpected (END-OF-INPUT wanted)'
But when I execute this query...
SELECT id, EXTRACTVALUE(content, '//a[contains(text(), "View")]/#href') AS url FROM tbl
...the query succeeds.
Why does the UPDATE query fail where the standalone SELECT query succeeds?