1064 error during joining tables - mysql

SELECT School_info.district_id,count(School_info.no_of_class) ls
from School_info where school_level IN('ls')
JOIN
SELECT t.district_id,count(t.no_of_class) p from School_info as t
where school_level IN('ps')
ON
( School_info.district_id = t.district_id)
Where is my error , i did not find it . when i ran the query above , it shows as : -
MySQL said: Documentation
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 'JOIN SELECT t.district_id,count(t.no_of_class) p from
School_info as t where sc' at line 2

Your query has a lot of syntax errors, but in any case it can be simplified as this:
select
district_id
, sum(school_level = 'ls') ls
, sum(school_level = 'ps') p
from school_info
where school_level in ('ls','ps')
group by district_id
The sum(school_level = 'p') uses the fact that MySQL does a boolean evaluation of the expression inside the sum() which returns 1 if true. Summing all 1s gives the same result as counting rows.
This will give you an output like this:
district_id ls p
where ls and p are counts.

Related

Where is my mistake selecting coalesce mysql?

I have a column discount_amount, if there's no SUM(discount_amount) (NULL) THEN it's zero (0).
I wrote the query with this
SELECT COALESCE(SUM(discount_amount)
FROM order_discount
WHERE order_discount.discount_type_id = 6
AND order_discount.order_match_id = om1.id, 0);
but I get an error
Error Code: 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 order_discount WHERE order_discount.discou' at line 2
You seem to want:
SELECT COALESCE(SUM(discount_amount), 0)
FROM order_discount
WHERE order_discount.discount_type_id = 6 AND order_discount.order_match_id = om1.id;
This has to be part of bigger query, where derive table om1 is defined somehow.
Alternatively, you can COALESCE() the result of the subquery like this:
COALESCE(
(
SELECT SUM(discount_amount)
FROM order_discount
WHERE order_discount.discount_type_id = 6 AND order_discount.order_match_id = om1.id
),
0
)
Again, this only makes sense if included in a bigger query.

calculate profit 10% from buy price

I have this script calculates profit
Steps :
barangbeli = harsat / diameter
p1 = barangbeli * 10
p2 = prof / 100
result = barangbeli + profit;
thanks
CREATE VIEW tbkeluar as
SELECT mbarang.kdbrg, mbarang.nmbrg, mbarang.spek,if(SUM(bkeluar.qty), SUM(bkeluar.qty), 0)as qty,(tbmasuk.harsat/mbarang.diameter) as hargabeli, ((hargabeli*10)/100 )+hargabeli) as profit
LEFT JOIN bkeluar on mbarang.kdbrg = bkeluar.kdbrg group by mbarang.kdbrg
i have 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 ') as profit from mbarang
LEFT JOIN bkeluar on mbarang.kdbrg = bkeluar.kdbrg gro' at line 2
The error message you received should be helpful enough to know whats wrong.
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 profit from mbarang LEFT JOIN bkeluar on mbarang.kdbrg = bkeluar.kdbrg gro' at line 2
Based from the error message, MySQL says that you have a syntax error because of this character ), with some characters appended for you to locate it: ) as profit from mbarang
As you can see from your SELECT statement, you have an extra )
SELECT mbarang.kdbrg
, mbarang.nmbrg
, mbarang.spek
,if(SUM(bkeluar.qty), SUM(bkeluar.qty), 0)as qty
,(tbmasuk.harsat/mbarang.diameter) as hargabeli
, ((hargabeli*10)/100 )+hargabeli) as profit
^ delete this extra parenthesis
Your SELECT statement (in CREATE VIEW) is missing its FROM clause. Do you mean FROM mbarang?
You're also probably missing at least one more JOIN; the table tbmasuk is mentioned in the SELECT clause, but isn't mentioned anywhere else in the statement.

remove duplicate records syntax error in SQL query

i try to remove duplicate records from table
my sql query is :
DELETE FROM products_description AS t1 WHERE EXISTS (
SELECT 'products_id', 'site_language_id'
FROM products_description AS t2
WHERE t2.products_id = t1.products_id
AND t2.site_language_id = t1.site_language_id
);
it give this 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 'as t1
where exists (select 'products_id','site_language_id' from products_desc' at line 1
i don't know what is missing ?
To delete all records which have duplicates
DELETE FROM products_description
WHERE product_id IN (
SELECT product_id FROM( Select product_id,site_language_id
From products_description
Group By product_id,site_language_id
Having Count(*)>1
) As X
)As Y
MySQL is complaining since there is a syntax error. Try replacing the quote (') with a backtick (`).
Be careful though! The query will then work but will delete all rows rather than just duplicates....

Using COUNT and DISTINCT keywords together to count distinct values

I am writing the query to count the number of distinct values in the database table. So I wrote the query as:
select count ( distinct user_id ) as unique_users from 1_time_access;
I am getting error
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 'distinct user_id ) as unique_users from 1_time_access' at line 1
Can anyone explain what am I missing?
Remove the space between count and the open parenthesis.
select count(distinct user_id) as unique_users from 1_time_access;

Mysql join 2 database and 3 tables query?

SELECT db1_t1.userid as userid
, db1_t1.customer_id as vw_customer
, db2_t1.customers_id as customer
, db2_t1.orders_id as order
FROM database1.table1 db1_t1
LEFT JOIN database2.table1 db2_t1
ON db1_t1.customer_id = db2_t1.customers_id
It gives me 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 'order FROM
database1.table1 db1_t1 LEFT JOIN
database2.' at line 2
I am using php and mysql.
order is a keyword - think ORDER BY my_column.
I'd suggest renaming it, but you could enclose it in backticks
db2_t1.orders_id AS `order`