Why is this SQL incorrect (MySQL)? - php

I have a following query:
"DELETE * FROM Participations WHERE scheduleId IN (SELECT id FROM Schedule WHERE meetingId = :meetingId) AND userId = :userId"
I assign variables using PDO properly.
In response I get:
SQLSTATE[42000]: Syntax error or access violation: 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 '* FROM Participations WHERE scheduleId IN (SELECT id FROM Schedule WHERE meeting' at line 1}
It is enough that I replace first DELETE with SELECT and query is done properly.
Besides when I test directly in phpMyAdmin the same query with DELETE in it works fine.
I am confused....

Use DELETE FROM (note the lack of *) instead.

The error message clearly indicates that it is a syntax error near '*'.
Remove * from your Delete statement
DELETE FROM Participations WHERE scheduleId IN
(SELECT id FROM Schedule WHERE meetingId = :meetingId) AND userId = :userId

Related

How to get empty column and null columns in laravel?

I have a column with null values in it and one is empty. Now I am trying to fetch both but it shows me an SQL syntax error. Please guide me on how can i fetch both empty and null columns.
SQLSTATE[42000]: Syntax error or access violation: 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 ',,,,,,42450,42706,42962,43218,43474,43730,43994,44250,44506,44762,45018,45274,45' at line 1 (SQL: select token_id, email_state, email_event_time, created_at as email_created_at from email_tracker where transaction_id = 2856 and email_tracker.id IN (SELECT max(id) FROM email_tracker WHERE token_id IN (,,,,,,,42450,42706,42962,43218,43474,43730,43994,44250,44506,44762,45018,45274,45530,45793,46049,46305,46561,46817,47073,47329,47585,47841,48097,48353,48609,48880,49136,49392,49648,49904,50160,50700,50956,51212,51468,51724,51980,52236,52492,52748,53004,53260,53516) GROUP BY token_id) order by created_at desc)
$this->replies->where($field_name,'=', '')->orWhereNull($field_name);
Please guide me on what I am doing wrong here.
Any solution appreciated!

Database Error PDOException

I created my database tables and bake as usual with CakePHP 3 but when I point my browser to some link it give me this errors:
Error: SQLSTATE[42000]: Syntax error or access violation: 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 'AS Users__contact person, Users.Phoneno AS Users__Phoneno, Users.email AS `U' at line 1
If you are using SQL keywords as table column names, you can enable identifier quoting for your database connection in config/app.php.
SQL Query:
SELECT Users.id AS `Users__id`,
Users.name AS `Users__name`,
Users.address AS `Users__address`,
Users.contact person AS `Users__contact person`,
Users.Phoneno AS `Users__Phoneno`,
Users.email AS `Users__email`
FROM users Users
LIMIT 20 OFFSET 0
Thanks guys and #RiggsFolly nice to have observerd that in the SQL
The error was from the space in the column contact person instead of contactperson in the SQL

Use of CONCAT in FROM clause

I have a non-working query as the sub-query FROM clause doesn't understand CONCAT():
SELECT *
FROM `events` e
WHERE EXISTS (SELECT *
FROM CONCAT('prefix_', e.`event_id`) registrations
WHERE registrations.`attendee` = 123456
)
Is there any way to make this work in a single-statement?
The error message I receive is:
#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 '('prefix_', e.event_id) registrations
You are trying to dynamically come up with a table name? I don't think this is possible...because doesn't the FROM clause table name need to be resolved prior to the SQL being able to be evaluated and executed?
SELECT * FROM (SELECT *,
CONCAT('prefix_', `event_id`) as prefix FROM `events`
WHERE events.attendee = 123456
);

MySQL NOT EXITS condition not working

Hello I am trying to execute a mysql query but getting error which I can't understand. The query I am using is
INSERT INTO summary (oid,tab,cost) VALUES('1','7','40') WHERE NOT EXISTS (SELECT * FROM summary WHERE cusid ='1')
I am using this tutorial as my reference, http://www.techonthenet.com/mysql/exists.php
This is the structure of the summary table
This is the error message
#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 new 'WHERE NOT EXISTS (SELECT * FROM summary WHERE cusid = 1)' at line 1
It is not exits. Change it to exists.
INSERT INTO summary (oid,tab,cost) VALUES('1','7','40') WHERE NOT EXISTS (SELECT * FROM summary WHERE cusid ='1')
^
Also you can't use NOT EXISTS with your above query, you can with a subquery.

Wrong syntax (MySQL, PHP)

This line of code
$SQL = "SELECT * FROM stats ORDER BY Team WHERE Team='$teamval'";
is returning with the following MySQL 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 'WHERE Team='OTT''
at line 1
I can't find anything wrong with the syntax, what's wrong with it?
The ORDER BY clause must appear after the WHERE clause. So, your query should, instead, be:
SELECT * FROM stats WHERE Team='$teamval' ORDER BY Team
You have used in correct syntax of using order by before where clause , ORDER BY should be used at the end of query if you have used limit in your query then put order by before limit
SELECT * FROM stats WHERE Team='$teamval' ORDER BY Team
Replace:
$SQL="SELECT * FROM stats WHERE Team='$teamval' ORDER BY Team";

Categories