SQL Syntax error in EXPLAIN - mysql

I'm trying to get an explain command to work in mysql. I am trying to troubleshoot the performance of this delete query:
DELETE FROM boost_cache_relationships
WHERE base_dir = 'cache/normal/www.dane101.com'
AND page_callback = 'node'
AND page_type = 'story'
AND page_id = '2891';
This query is the output from a SHOW FULL PROCESSLIST command.
I understand that EXPLAIN does not work with delete so I copied it and replaced DELETE with SELECT to give the following:
explain select
FROM boost_cache_relationships
WHERE base_dir = 'cache/normal/www.dane101.com'
AND page_callback = 'node'
AND page_type = 'story'
AND page_id = '2891';
When I hit enter, mysql gives me an error message that this is invalid SQL:
ERROR 1064 (42000): 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 boost_cache_relationships WHERE base_dir =
'cache/normal/www.dane101.com' A' at line 1
What am I doing wrong?

You need to specify a field list:
select * FROM boost_cache_relationships WHERE base_dir = 'cache/normal/www.dane101.com' AND page_callback = 'node' AND page_type = 'story' AND page_id = '2891';

A select query needs column names to select. A delete query doesn't.
SELECT *
FROM boost_cache_relationships
WHERE base_dir = 'cache/normal/www.dane101.com'
AND page_callback = 'node'
AND page_type = 'story'
AND page_id = '2891';

In your select try to select some columns:
SELECT *...

Related

how to alias(AS) a table in raw SQL with knex.js

I am using knex.js with MySql DB. I have a migration where I have to use raw SQL. My migration looks like this :
import * as Knex from "knex";
const brokersTable = "OriginationAdmin.Brokers";
const brokerIndividualTable = "OriginationAdmin.BrokerIndividuals";
exports.up = async (knex: Knex) => {
await knex.schema.raw(
`UPDATE ${brokersTable}
JOIN(
SELECT CompanyName, MIN(Id) AS Id
FROM ${brokersTable}
GROUP BY CompanyName )
AS x
ON x.CompanyName = ${brokersTable}.CompanyName
AND isDeleted IS NULL
SET isDeleted = CASE WHEN (x.Id <> ${brokersTable}.Id) THEN 1 END`
);
when I run migrate up I get an error message:RequestError: Incorrect syntax near the keyword 'AS'. I know the SQL Query is syntactically correct but why am I getting an error with the aliasing keyword and how do I fix it?
Edit: This most(relevant part) of the error message
migration file "20210822190619_de-duplicate-brokers-and-associate-individuals.ts" failed
migration failed with error: UPDATE OriginationAdmin.Brokers
JOIN (
SELECT CompanyName, MIN(Id) AS Id
FROM OriginationAdmin.Brokers
GROUP BY CompanyName
) AS x
ON x.CompanyName = OriginationAdmin.Brokers.CompanyName
AND isDeleted IS NULL
SET isDeleted = CASE WHEN (x.Id <> OriginationAdmin.Brokers.Id) THEN 1 END
- Incorrect syntax near the keyword 'AS'.
RequestError: Incorrect syntax near the keyword 'AS'.
at handleError (/Users/haroonAzhar/Desktop/FAL/omni-api/node_modules/mssql/lib/tedious/request.js:366:15)
The error message isn't very helpful to what the problem is.

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')
);

Yii2: can't create sql command using innerJoin

I am not that good at SQL but i do as much as i can for the little knowledge i have..
I have made a single a flat SQL string with the help from a friend that gathers data from a table using a relative table from an initial data from the first table, the SQL was made like this:
SELECT id, username, auth_assignment.created_at
FROM `user` JOIN `auth_assignment`
ON (user.id = auth_assignment.user_id)
JOIN `auth_item`
ON (auth_item.name = auth_assignment.item_name)
WHERE auth_item.name = 'Admin'
the initial data to look is Admin so everything works in that side, but i tried to simulate this SQL using Yii2 functions.. so far i have made this code
$query = new Query;
$command = $query->select('id, username')
->innerJoin('auth_assignment', ['user.id'=>'auth_assignment.user_id'])
->innerJoin('auth_item', ['auth_item.name'=>'auth_assignment.item_name'])
->where('auth_item.name = :name', [':name'=>$name])->createCommand();
var_dump($command->query());
this returns an SQLSTATE 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 'INNER JOIN `auth_assignment` ON `user`.`id`='auth_assignment.user_id' INNER JOIN' at line 1
The SQL being executed was: SELECT `id`, `username` INNER JOIN `auth_assignment` ON `user`.`id`='auth_assignment.user_id' INNER JOIN `auth_item` ON `auth_item`.`name`='auth_assignment.item_name' WHERE auth_item.name = 'Admin'
i checked the method $command->sql; to know how the SQL was being generated.. but i really don't know how to fix it due to my lack of my knowledge to SQL and lack of understanding to yii2 api documentation
SQL is generated like this:
SELECT `id`, `username` INNER JOIN `auth_assignment` ON `user`.`id`=:qp1 INNER JOIN `auth_item` ON `auth_item`.`name`=:qp2 WHERE auth_item.name = :name
i appreciate any help
Try This Query
$query = (new yii\db\Query())
->select('id, username, auth_assignment.created_at')
->from('user')
->innerJoin('auth_assignment','user.id=auth_assignment.user_id')
->innerJoin('auth_item','auth_item.name = auth_assignment.item_name')
->where([
'auth_item.name' => 'Admin'
])->all();

Nested MySQL syntax error trying to add division operator

I have a query that looks like this that executes and works perfectly:
UPDATE users
SET player_skill = (
(
SELECT
sum(avg_score)
FROM
skill
WHERE
username = "JohnDoe"
AND game = "Bingo"
) -
(
SELECT
sum(avg_score)
FROM
events
WHERE
game = "Bingo"
)
) WHERE username = "JohnDoe"
I want to add a division operator like this:
UPDATE users
SET player_skill = (
(
(
SELECT
sum(avg_score)
FROM
skill
WHERE
username = "JohnDoe"
AND game = "Bingo"
) - (
SELECT
sum(avg_score)
FROM
events
WHERE
game = "Bingo"
)
) / SELECT
COUNT(*)
FROM
skill
WHERE
username = "JohnDoe"
AND game = "Bingo"
)
WHERE
username = "JohnDoe"
It spits out an error code at me
[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 'COUNT(*)
FROM
skill
WHERE
username = "JohnDoe"
AND game = "Bingo" )
) W' at line 21
I've tried for awhile but can't seem to figure out why it wont let it run. I have similar queries to this that seem fine but I cannot get this one to work.
Any help would be greatly appreciated.
try this
UPDATE users
SET player_skill = (
SELECT (sum(s.avg_score) - sum(e.avg_score))/COUNT(e.*)
FROM skill s
INNER JOIN events e on s.game = e.game
WHERE e.username = "JohnDoe"
AND e.username = "Bingo"
)
when you try this query you will get this error :
[Err] 1064 - You have no error in your SQL syntax. you dont need to check the manual that corresponds to your MySQL server version for the right syntax to use no near anything .

Sql error occurred when I'm trying this query

Here is the query
$query_order = "select * from orders where key = '$pay_key'";
Error shown
SELECT
*
FROM `orders`
where `key` = 'C90320'
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 'key = 'C90320'' at line 1*
key is a reserved word. Change your query to:
$query_order = "select * from orders where `key` = '$pay_key'";
Also, I would recommend escaping the $pay_key's value. Say something like:
$pay_key = mysqli_real_escape_string($pay_key);
$query_order = "select * from orders
where `key` = '".mysqli_real_escape_string($pay_key)."'";
try this
first of all you need to compare it using string so code should like this
$query_order = "select * from orders where `key` = '".$pay_key."'";