MySQL Subqueries Not Working, Generating Error Code 1064 - sql

This query doesn't work
SELECT * FROM Catalogue WHERE Catalogue.ID IN (
SELECT ID_catalogue FROM Categories WHERE Categories.Product_type='xxx'
)
Error Code : 1064
You have an error in
your SQL syntax near 'SELECT
ID_catalogue FROM Categories WHERE
Categories.Product_type='xxx' ) ' at
line 2
Ok, that's because I am using a very old MySQL version.
What I am trying to get is
SELECT * FROM Catalogue WHERE Product_type='xxx' OR Catalogue.ID IN (
SELECT ID_catalogue FROM Categories WHERE Categories.Product_type='xxx'
)
Is there any equivalent for that?
Thank you for all your comments.

If you're using mysql version <= 4.0 - then it is the reason, since subqueries were added in 4.1
SELECT c.*,
g.ID_catalogue
FROM Catalogue c
LEFT JOIN Categories g ON g.ID_catalogue = c.ID
AND g.Product_type='xxx'
HAVING ID_catalogue IS NOT NULL
OR Product_type = 'xxx'
You should add composite index ID_catalogue + Product_type for table Categories AND index Product_type for table Catalogue to have this query performed fast

There is nothing wrong with your query. It is the version of MySQL that is more than likely your problem.

Related

Postgresql Syntax for a Left Join Throwing Syntax Error

I have a joa_clinics table which has a field grouper_id and that field is reflected as pcp_grouper_id in a joa_sales table. What I need is to get all the values in the joa_sales table with an additional dynamic column joa which would be 0 or 1 depending on whether the joa_sales table has pcp_gropuer_id value as null or not. Here is my query so far but it generates a syntax error on the second select:
SELECT * FROM joa_sales,
SELECT CASE WHEN g.GROUPER_ID IS NOT NULL THEN 1 ELSE 0 END AS JOA
FROM joa_clinics exp LEFT JOIN ( SELECT DISTINCT GROUPER_ID FROM joa_clinics ) g ON exp.PCP_GROUPER_ID = g.GROUPER_ID;
Maybe I don't even need to reference the joa_clinics table but don't know the syntax. The above sql is copied from a SQL Server syntax but mine is a Postgresql server.
Thank you!
Here is I think will work for me. No Join was needed:
SELECT * , CASE WHEN js.pcp_grouper_id IS NOT NULL THEN 1 ELSE 0 END AS JOA
FROM joa_sales js;

SQL Many-to-Many Query in Apex Oracle

I created a many-to-many relationship between the tables PRODUCT_TYPE and LABEL by creating and intermediate table PRODUCT_TYPE-LABELS. I wanted to retrieve all the products that have the labels 'Gluten free' and 'Lactose free' and found help on a similar subject (How to filter SQL results in a has-many-through relation) but never got it to work properly.
The tables are as follows:
PRODUCT_TYPE{
PRODUCT_TYPE ->Primary Key
CONTAINER
VOLUME_L
PRICE_EUROS
...
}
LABEL{
LABEL_NAME ->Primary Key
REQUIREMENTS
}
PRODUCT_TYPE-LABELS{
PRODUCT_TYPE
LABEL_NAME
}
In fact, even when creating the simplest command
SELECT PRODUCT_TYPE-LABELS.PRODUCT_TYPE
FROM PRODUCT_TYPE-LABELS
I obtain the following error ORA-00933: SQL command not properly ended that I can't solve. I'm working on Apex Oracle (Required for this course).
Thanks !
If your table is really named PRODUCT_TYPE-LABELS then you need to enclose it within double quotes. - is not a standard character that is allowed in table names so to use special characters such as that, you need to put quotes around the table. I would recommend AGAINST using a table name such as that and maybe use something like PRODUCT_TYPE_LABEL.
Does the query work from SQL Developer, SQLPlus or any other tool that you can use to run queries?
Try running the query:
SELECT PRODUCT_TYPE
FROM "PRODUCT_TYPE-LABELS"
select * from (
select rownum rowno, CUST_ID,CUST_NAME,no_of_orders,orders_amount,EMAIL from (
select mst.CUST_ID, mst.CUST_NAME, count(mst.INVONO) no_of_orders, sum(SUB_TOTAL) orders_amount, au.EMAIL
from sales_mst mst,CUSTOMER_INFO cust, APP_USERS au
where cust.USER_NAME = au.USERNAME
and cust.cust_id=mst.cust_id
and (au.gender= :P41_GENDER or :P41_GENDER is null)
and (au.DOB = :P41_DOB or :P41_DOB is null)
group by mst.CUST_ID, mst.CUST_NAME,au.EMAIL
having nvl(sum(SUB_TOTAL),0) > 0
order by 3 desc,4 desc
)) where rowno <=:P41_TO_CUSTOMER
/*select * from (
select rownum rowno, CUST_ID,CUST_NAME,no_of_orders,orders_amount from (
select mst.CUST_ID, mst.CUST_NAME, count(mst.INVONO) no_of_orders, sum(SUB_TOTAL) orders_amount
from sales_mst mst
group by mst.CUST_ID, mst.CUST_NAME
having nvl(sum(SUB_TOTAL),0) > 0
order by 3 desc,4 desc
)) where rowno <=:P41_TO_CUSTOMER
*/

Opencart - phpMyAdmin Bulk update Special Price

I work as administrator in one electronic shop we are using OpenCart eCommerce and recently they asked me to add special price to Bulk products and leave it running for some days. Of course i can change it in admin page one by one. The things is how i can change it through phpMyAdmin.
( Opencart Version 2.1.0.1 )
I have 46 products which cost 25€ and i have to add special price to 20€.
Table we have to update price is
oc_product_special
and column we have to update is
oc_product_special.price
Of course we need products which cost 25€ from table
oc_product
to retrieve products from this table and add special price to 20€
The thing is how to connect these two tables so i can add special price which sql query to run so i can achieve this.
oc_product & oc_product_special , update this column oc_product_special.price
I 've tried to run this sql query but i receive error
UPDATE `oc_product_special`.price
SET `oc_product_special`.price = 16.1290
FROM `oc_product`
INNER JOIN `oc_product_special`
ON `oc_product`.product_id =`oc_product_special`.product_id
WHERE`oc_product`.price = 20.1612;
**** Price is without taxes 25€ / 1,24(tax rate) = 20.1612€
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 'FROM oc_product INNER JOIN oc_product_special ON oc_product.product_id =' at line 2
Any help is appreciated
** EDIT
Updating my Sql query
UPDATE oc_product_special AS ps
INNER JOIN oc_product op ON ps.product_id = op.product_id
set ps.price = 16.1290
where op.price = 20.1612
but the thing is that 0 rows affected. (Query took 0.0003 sec) , if i run select query with these prices i have results.
select *
from oc_product as op
where op.price=20.1612
Showing rows 0 - 29 (46 total, Query took 0.0010 sec)
***** Updated 2nd time
Firstly i ran
DELETE FROM `oc_product_special`
WHERE product_id IN
(
SELECT product_id
FROM oc_product p
WHERE p.price = 20.1612
);
so i can " remove any existing specials from these products " , this query ran without any issue
After that i tried to ran Insert query
INSERT INTO `oc_product_special` (`product_special_id`, `product_id`, `customer_group_id`, `priority`, `price`, `date_start`,`date_end`)
VALUES
(
SELECT NULL, 78, 1, 0, 16.1290, 0000-00-00, 0000-00-00
FROM oc_product p
WHERE p.price = 20.1612
);
I adjusted my data concerning to my table's data. But i receive 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 'SELECT NULL, 78, 1, 0, 16.1290, 0000-00-00, 0000-00-00 FROM oc_product p ' at line 4
Your query has no formatting and does not correspond to MySQL standards, you have to pay attention to the following:
After an UPDATE statement one should specify the table to use and not the column
The SET statement only comes after an INNER JOIN
Use suffixes for convenience when joining tables
Correct MySQL query below (change the prices):
UPDATE oc_product_special AS ps
INNER JOIN oc_product op ON ps.product_id = op.product_id
set ps.price = 22 where op.price = 0.00
You need an insert statement rather than an update. You might also consider removing any existing specials from these products beforehand and run a query to clear them like this:
DELETE FROM `oc_product_special`
WHERE product_id IN
(
SELECT product_id
FROM oc_product p
WHERE p.price = 20.1612
);
Then you can select the desired product ids along with other fields and insert them like this:
INSERT INTO `oc_product_special` (`product_special_id`, `product_id`, `customer_group_id`, `priority`, `price`, `date_start`, `date_end`)
VALUES
(
SELECT NULL, product_id, 1, 0, 16.1290, CURDATE(), CURDATE() + INTERVAL 1 DAY
FROM oc_product p
WHERE p.price = 20.1612
);
In this example the special starts today and runs for 5 days. Of course you should adjust the customer_group_id, priority,start_date, andend_date` in my query.

Need help with error in Oracle SQL query

this is the query:
SELECT DISTINCT pprom.pk
FROM
(
SELECT
item_t0.SourcePK as pk
FROM
links item_t0
WHERE (? = item_t0.TargetPK AND item_t0.SourcePK in (?,?))
AND (item_t0.TypePkString=? )
UNION
SELECT
item_t1.TargetPK as pk
FROM
cat2prodrel item_t1
WHERE ( item_t1.SourcePK in (? ) AND item_t1.TargetPK in (?,?))
AND (item_t1.TypePkString=? )
) AS pprom
And this is the error:
ORA-00933: SQL command not properly ended
Any ideas what could be wrong?
Edit:
The question marks are replaced by PKs of the respective items:
values = [PropertyValue:8802745684882, PropertyValue:8796177006593, PropertyValue:8796201713665, 8796110520402, PropertyValue:8796125954190, PropertyValue:8796177006593, PropertyValue:8796201713665, 8796101705810]
Edit 2:
The query is executed deep inside some proprietary software system so I don't know exactly the code that runs it.
Edit 3:
I found one more query that's a little shorter but results in the same error message:
SELECT DISTINCT pprom.pk
FROM
(
SELECT
item_t0.SourcePK as pk
FROM
links item_t0
WHERE (? = item_t0.TargetPK AND item_t0.SourcePK in (?))
AND (item_t0.TypePkString=? )
) AS pprom
Using the following values:
values = [PropertyValue:8799960601490, PropertyValue:8796177006593, 8796110520402]
Edit 4
I found the SQL code that is sent to the db after replacing the values:
SELECT DISTINCT pprom.pk
FROM
(
SELECT
item_t0.SourcePK as pk
FROM
links item_t0
WHERE (8801631769490 = item_t0.TargetPK AND item_t0.SourcePK in (8796177006593))
AND (item_t0.TypePkString=8796110520402 )
) AS pprom
I also tried executing the inner SELECT statement and that alone runs ok and returns a single PK as a result.
I could not find any obvious syntax error in your query so I'd presume the issue is the client library you are using to convert the ? place holders into actual values. Your question edit displays a sort of dump where there are 8 integers but only 6 PropertyValue items. Make sure that's not the issue: IN (?, ?) requires 2 parameters.
Edit
Try removing the AS keyword when you assign an alias to the subquery:
SELECT DISTINCT pprom.pk
FROM
(
SELECT
item_t0.SourcePK as pk
FROM
links item_t0
WHERE (8801631769490 = item_t0.TargetPK AND item_t0.SourcePK in (8796177006593))
AND (item_t0.TypePkString=8796110520402 )
) AS pprom
^^

Mysql, NOT EXISTS, SELECT

Well, I was trying to select rows from one table if there are no rows in another table.
My original query was:
SELECT * FROM `jos_datsogallery` as a WHERE a.published = 1
and a.approved=1 NOT EXISTS (SELECT * FROM `jos_datsogallery_votes`
As v WHERE v.vip=62 AND v.vpic=a.id) ORDER BY a.imgdate DESC
but it keeps failing.
I made some testing and shortened my query to:
SELECT * FROM `jos_datsogallery` WHERE EXISTS (SELECT 1)
Which is supposed to select everything from jos_datsogallery as 'EXISTS (SELECT 1)' is always true.
I tried phpMyAdmin:
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 'EXISTS (SELECT 1) LIMIT 0, 30'
at line 1
What's wrong?
MySQL version: 4.0.27
MySQL doc: http://dev.mysql.com/doc/refman/4.1/en/exists-and-not-exists-subqueries.html
EXISTS is only supported in 4.1 and above - the documentation you linked is a combined documentation for both 4.0/4.1 so it may be misleading as to what versions actually support the keyword.
Since you updated your question to state that you're using 4.0.x that would be why it's not working for you.
Here is another way you can achieve the same, without using NOT EXISTS.
SELECT * FROM `jos_datsogallery` AS a
LEFT JOIN `jos_datsogallery_votes` AS v ON v.vip=62 AND v.vpic=a.id
WHERE a.published = 1 AND
a.approved=1 AND
v.vip IS NULL
ORDER BY a.imgdate DESC
Using a left join means the right-hand of the join (the jos_datsogallery_votes part) is allowed to not find any rows while still returning a result. When the right hand side of the join is not found, its columns will all have a value of NULL, which you can check on in the WHERE part of the query.
HTH
A little late, but I was searching for something similar and it might be of use to someone else. Another way to do this, is to use a count in the where clause. So, your query would be:
SELECT * FROM `jos_datsogallery` AS a
WHERE a.published = 1
AND a.approved=1
AND (
SELECT COUNT(*) FROM `jos_datsogallery_votes` AS v
WHERE v.vip=62
AND v.vpic=a.id
) = 0
ORDER BY a.imgdate DESC