Another 1064 syntax error - mysql

I just can't figure out what's wrong with this syntax. I get a #1064 syntax error at the last line.
SELECT b.id, b.lastname, b.name, c.balance, a.maxdebt, b.warndata, b.warndownload, b.warnupload, b.warndebt, b.cutoffdata, b.cutoffdownload, b.cutoffupload, b.cutoffdebt, b.data, b.download, b.upload, b.warning, b.access, b.cutoffstop
FROM (
SELECT customers.id AS id, SUM(tariffs.value) AS maxdebt
FROM tariffs
INNER JOIN assignments ON tariffs.id = assignments.tariffid
INNER JOIN customers ON assignments.customerid = customers.id
GROUP BY id
) a
LEFT JOIN (
SELECT customers.id AS id, UPPER(lastname) AS lastname, customers.name AS name, SUM(stats.upload+stats.download) AS data, SUM(stats.download) AS download, SUM(stats.upload) AS upload, cutoffstop, warndata, warndownload, warnupload, warndebt, cutoffdata, cutoffdownload, cutoffupload, cutoffdebt, warning, access
FROM customers
LEFT JOIN nodes ON customers.id = nodes.ownerid
LEFT JOIN stats ON nodes.id = stats.nodeid
LEFT JOIN customerwarnings ON customers.id = customerwarnings.id
GROUP BY id
) b
LEFT JOIN (
SELECT customerid, SUM(cash.value) AS balance
FROM cash
GROUP BY customerid
) c ON a.id = b.id = c.customerid
I've tried to join the tables with RIGHT and LEFT JOINS to see if there is a difference, but they give me the same error.
The different queries within work fine on themselves, but when I join them together in this query I get a syntax error. Does anyone have an idea what I should or shouldn't do?
The error: "#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 '' at line 21"

Here is the reference for Multiple LEFT JOIN
SELECT b.id, b.lastname, b.name, c.balance, a.maxdebt, b.warndata, b.warndownload, b.warnupload, b.warndebt, b.cutoffdata, b.cutoffdownload, b.cutoffupload, b.cutoffdebt, b.data, b.download, b.upload, b.warning, b.access, b.cutoffstop
FROM (
SELECT customers.id AS id, SUM(tariffs.value) AS maxdebt
FROM tariffs
INNER JOIN assignments ON tariffs.id = assignments.tariffid
INNER JOIN customers ON assignments.customerid = customers.id
GROUP BY id
) a
LEFT JOIN (
SELECT customers.id AS id, UPPER(lastname) AS lastname, customers.name AS name, SUM(stats.upload+stats.download) AS data, SUM(stats.download) AS download, SUM(stats.upload) AS upload, customers.cutoffstop, warndata, warndownload, warnupload, warndebt, cutoffdata, cutoffdownload, cutoffupload, cutoffdebt, nodes.warning, nodes.access
FROM customers
LEFT JOIN nodes ON customers.id = nodes.ownerid
LEFT JOIN stats ON nodes.id = stats.nodeid
LEFT JOIN customerwarnings ON customers.id = customerwarnings.id
GROUP BY id
) b ON a.id = b.id
LEFT JOIN (
SELECT customerid, SUM(cash.value) AS balance
FROM cash
GROUP BY customerid
) c ON a.id = c.customerid

Related

SQL update table using result of another query

I am trying to update the table(orders)after calculate the total amount of that order, I successfully calculate the total amount, but I cannot find way to update the table using the result.
code I successfully calculate the total amount:
SELECT orders.orderid,
SUM(ordersdish.quantity*dish.price) AS total
FROM dish
JOIN ordersdish
ON ordersdish.dishid = dish.dishid
JOIN orders
ON orders.orderid = ordersdish.orderid
GROUP BY orders.orderid;
Result:
total amount
Code I tried to update table(orders):
UPDATE orders
SET total = t1.total
FROM (
SELECT orders.orderid,
SUM(ordersdish.quantity*dish.price) AS total
FROM dish
JOIN ordersdish
ON ordersdish.dishid = dish.dishid
JOIN orders
ON orders.orderid = ordersdish.orderid
GROUP BY orders.orderid
)t1
WHERE orders.orderid = t1.orderid;
MySQL said:
#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 'from
(select orders.orderid, SUM(ordersdish.quantity*dish.price)as total
from ' at line 3
And here is the table "orders":
The correct syntax in MySQL use JOIN:
update orders o join
(select od.orderid, SUM(od.quantity * d.price)as total
from dish d join
ordersdish od
on od.dishid = d.dishid
group by od.orderid
) t1
on o.orderid = t1.orderid
set o.total = t1.total;
Note that orders is not needed in the subquery, because the orderid is in orderdish.
Please try this answer.
UPDATE o
SET o.total = t1.total
(SELECT o.orderid, SUM(od.quantity * d.price) AS total
FROM dish AS d
JOIN ordersdish AS od ON od.dishid = d.dishid
JOIN orders AS o ON o.orderid = od.orderid
GROUP BY orders.orderid) t1
WHERE o.orderid = t1.orderid;

How to remove some data not needed on new row from mysql query with rollup

Hello I have a problem with the rollup mysql query. I want to get total of each column. I use mysql SUM() and ROLLUP to get the result. But on the new row that is added it copies the data of the last row. How can I remove those data. I provided an image on what I really needed to achieve.
Here is my query.
SELECT IFNULL(payments.id, "General Total"), payments.driver_id, payments.vehicle_specifications_id,payments.admins_id, payments.vehicle_id, payments.boundaries, payments.cashbond_payments, payments.loans, payments.penalties, SUM(payments.total_payments), a.first_name, a.mid_name, a.last_name, d2.date, d.first_name, d.mid_name, d.last_name, v.plate_number, v2.car_rate FROM payments INNER JOIN admins AS a ON payments.admins_id = a.id INNER JOIN drivers AS d ON payments.driver_id = d.id INNER JOIN dispatch AS d2 ON payments.dispatch_id =d2.id INNER JOIN vehicles AS v ON payments.vehicle_id = v.id INNER JOIN vehicle_specifications AS v2 ON payments.vehicle_specifications_id = v2.id GROUP BY payments.id WITH ROLLUP
Try this using union all
SELECT
payments.id,
payments.driver_id,
payments.vehicle_specifications_id,
payments.admins_id,
payments.vehicle_id,
payments.boundaries,
payments.cashbond_payments,
payments.loans,
payments.penalties,
SUM(payments.total_payments),
a.first_name,
a.mid_name,
a.last_name,
d2.date,
d.first_name,
d.mid_name,
d.last_name,
v.plate_number,
v2.car_rate
FROM payments
INNER JOIN admins AS a ON payments.admins_id = a.id
INNER JOIN drivers AS d ON payments.driver_id = d.id
INNER JOIN dispatch AS d2 ON payments.dispatch_id =d2.id
INNER JOIN vehicles AS v ON payments.vehicle_id = v.id
INNER JOIN vehicle_specifications AS v2 ON payments.vehicle_specifications_id = v2.id
GROUP BY payments.id
union all
select
'General Total' as id,
'' as driver_id,
'' as vehicle_specifications_id,
'' as admins_id,
'' as vehicle_id,
'' as boundaries,
'' as cashbond_payments,
'' as loans,
'' as penalties,
SUM(payments.total_payments),
'' as first_name,
'' as mid_name,
'' as last_name,
'' as date,
'' as first_name,
'' as mid_name,
'' as last_name,
'' as plate_number,
'' as car_rate
FROM payments
INNER JOIN admins AS a ON payments.admins_id = a.id
INNER JOIN drivers AS d ON payments.driver_id = d.id
INNER JOIN dispatch AS d2 ON payments.dispatch_id =d2.id
INNER JOIN vehicles AS v ON payments.vehicle_id = v.id
INNER JOIN vehicle_specifications AS v2 ON payments.vehicle_specifications_id = v2.id

Incorrect id returned on LEFT JOIN in Mysql statement

I need to get the id and timestamps of table sellers and all other columns (without knowing the column names) from these results returned from this MySql statement:
SELECT * FROM sellers a
LEFT JOIN users b ON a.user_id = b.id
LEFT JOIN country_types c ON a.country_type_id = c.id
LEFT JOIN language_types d ON a.language_type_id = d.id
WHERE a.email=?
The seller id though is incorrectly set because users, country_types, and language_types all have a value id. How can I set seller_id and seller_timestamp? I tried this but it is incorrect:
SELECT a.id seller_id, a.timestamp seller_timestamp, * FROM sellers a ...
You want this:
SELECT a.id as seller_id, a.timestamp as seller_timestamp, a.*, b.*, c.*, d.*
FROM sellers a
LEFT JOIN users b ON a.user_id = b.id
LEFT JOIN country_types c ON a.country_type_id = c.id
LEFT JOIN language_types d ON a.language_type_id = d.id
WHERE a.email=?
Im not sure but try alias, for example:
a.id AS seller_id
and etc.
In joins you can't select other columns in this way:
SELECT a.id seller_id, a.timestamp seller_timestamp, * FROM sellers a...
You need to write required column names.

CDbCommand - Three Tables Join and IN Clause Syntax

In my Yii application, I have following database structure:
Table user (id, firstName, lastName)
Table category (id, categoryName)
Table item (id, categoryId, name)
Table usergroup(id, userId, groupName)
I am trying to run following query:
$sql = 'SELECT a.id, a.firstName, a.lastName, b.id, b.categoryName, c.id, c.name
FROM user AS a
INNER JOIN (category AS b ON a.id = b.userId
WHERE b.userId IN (SELECT f.id FROM usergroup f WHERE f.userId=:currentUserId))
INNER JOIN item AS c ON b.id = c.categoryId
WHERE c.name LIKE :listedName';
$command=$connection->createCommand($sql);
$currentUserId = Yii::app()->user->id;
//binding :currentUserId
$command->bindParam(":currentUserId", $currentUserId,PDO::PARAM_STR);
$listedItemName = $data->name;
//binding :listedItemName
$command->bindParam(":listedName", $listedName,PDO::PARAM_STR);
$dataReader=$command->query();
However, I get following excption:
CDbException
CDbCommand failed to execute the SQL statement: 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 'ON a.id = b.userId WHERE b.userId IN
(SELECT f.id FROM usergroup' at line 3.
The SQL statement executed was:
SELECT a.id, a.firstName, a.lastName, b.id, b.categoryName, c.id, c.name FROM user AS a INNER JOIN (category AS b ON a.id = b.userId WHERE b.userId IN (SELECT f.id FROM usergroup f WHERE f.userId=:currentUserId)) INNER JOIN item AS c ON b.id = c.categoryId WHERE c.name LIKE :listedName. Bound with :currentUserId='2', :listedName='Oliver Twist'
Any sort of help or advice will be highly appreciated. Thank You!
You seem to have some parenthesis issue in there as well as possibly an unnecessary sub-select. I am guessing that what you are looking for is:
SELECT a.id, a.firstName, a.lastName, b.id, b.categoryName, c.id, c.name
FROM user AS a
INNER JOIN category AS b ON a.id = b.userId
INNER JOIN usergroup AS f ON f.userID = a.id
INNER JOIN item AS c ON b.id = c.categoryId
WHERE c.name LIKE :listedName'
AND f.userID=:currentUserId;
But, I am unclear on the join between item and category tables. You don't seem to to have foreign for category in items table and are just joining the item id and category id.
I am also unclear on your join between the user table and category table where you are saying the user id = category id. I don't see how these are related.
For update/reference of any one else facing same issue. I later figured out the problem that SQL WHERE CLAUSE should only be used at the end of statement, and I changed the first WHERE clause with AND condition. Here is the working query:
$sql = 'SELECT a.id, a.firstName, a.lastName, b.id, b.categoryName, c.id, c.name
FROM user AS a
INNER JOIN (category AS b ON a.id = b.userId
AND b.userId IN (SELECT f.id FROM usergroup f WHERE f.userId=:currentUserId))
INNER JOIN item AS c ON b.id = c.categoryId
WHERE c.name LIKE :listedName';

join two inline tables - unknown column?

I'm getting "Error Code: 1054. Unknown column 'E1.extensao' in 'field list'". I changed table names but my query is as follows:
SELECT E1.stateName, E1.city, E1.boughtEnough, E2.bought
FROM
(SELECT count(DISTINCT idClient),city,stateName FROM
(SELECT
max(n.ordervalue) as boughtEnough,
m.idClient,
i.nome AS city,
i.id AS cityId,
i.stateName
FROM Clients c
INNER JOIN client_order m ON c.idClient = m.idClient
INNER JOIN orders n ON m.client_order = n.client_order
INNER JOIN orderDetail p ON n.idorder = p.idorder
AND p.idCurso = m.idCurso
INNER JOIN cities i ON c.city = i.id
WHERE
m.idCurso = '10'
GROUP BY
m.idClient,
i.nome,
i.id,
i.stateName
HAVING max(n.orders) >= 6) t
GROUP BY t.city, t.stateName
ORDER BY t.stateName,t.city) E1
JOIN (SELECT count(DISTINCT idClient),city,stateName FROM
(SELECT
count(n.ordervalue) as bought,
m.idClient,
i.nome AS city,
i.id AS cityId,
i.stateName
FROM Clients c
INNER JOIN client_order m ON c.idClient = m.idClient
INNER JOIN orders n ON m.client_order = n.client_order
INNER JOIN orderDetail p ON n.idorder = p.idorder
AND p.idCurso = m.idCurso
INNER JOIN cities i ON c.city = i.id
WHERE
m.idCurso = '10'
GROUP BY
m.idClient,
i.nome,
i.id,
i.stateName
HAVING ((max(n.orders) < 6) AND (count(n.orders) >= 1))) t
GROUP BY t.city, t.stateName
ORDER BY t.stateName,t.city) E2 ON E1.cityId = E2.cityId
I'm more used to SQL Server, not MySQL. What am I getting wrong?
I assume that E1.extensao means E1.boughtEnough? Look closely at how E1 is defined:
(SELECT count(DISTINCT idClient),city,stateName FROM
(SELECT
max(n.ordervalue) as boughtEnough,
...) t
...) E1
There's a t.boughtEnough, but you're not "passing it up" to E1.