I get this error when accessing a website"Error while accessing the database You have an error in your SQL syntax" - sql

I keep getting this error:
"Error while accessing the database
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 '' at line 1
select count(*)
from course_catalog cc
INNER JOIN classes c ON cc.course_id = c.course_id
INNER JOIN reservation r ON c.class_id = r.class_id
where r.student_id ="
How can i fix it?

" is not a valid identifier. It seems likes you're supposed to pass a student id (an integer) to be compared with r.student_id.

You have " in the end of the sql query. Make your query properly by providing appropriate condition.

Change the last part, adding a value to compare against.
For example, against 123:
select count(*)
from course_catalog cc
INNER JOIN classes c ON cc.course_id = c.course_id
INNER JOIN reservation r ON c.class_id = r.class_id
where r.student_id = 123
Or against null:
select count(*)
from course_catalog cc
INNER JOIN classes c ON cc.course_id = c.course_id
INNER JOIN reservation r ON c.class_id = r.class_id
where r.student_id is null

Related

Oracle (Netsuite) SQL one join limit results

I have an oracle SQL query and a slight problem. I need to check if an item has a PO# that it has at least 1 line item. The query below works however it returns a result for each line of transaction_lines and I need only une result. PS I tried DISTINCT but get an ODBC error.
SELECT ITEMS.NAME, INVENTORY_NUMBER.INVENTORY_NUMBER, INVENTORY_NUMBER.ON_HAND_COUNT, ITEMS.SALESDESCRIPTION, CONDITION.LIST_ITEM_NAME,
BRAND_PARTNER.LIST_ITEM_NAME, PPROGRAM.LIST_ITEM_NAME, ENTITY.NAME, PO.TRANSACTION_NUMBER, INVENTORY_NUMBER.RECEIVED_COST, ITEMS.SALESPRICE, IR.TRANSACTION_NUMBER,
INVENTORY_SOURCE.LIST_ITEM_NAME, LOCATIONS.NAME, INVENTORY_NUMBER.RECEIPT_DATE, PO.INTERNAL_MEMO, INVENTORY_NUMBER.REFERENCE_, TEST_RESULTS.LIST_ITEM_NAME,
INVENTORY_NUMBER.TEST_FILE_LINK, INVENTORY_NUMBER.CONNECT_TRADE_ID, INVENTORY_NUMBER.SOLD_DATE, INVENTORY_NUMBER.SOLD_PRICE, INVENTORY_NUMBER.MEMO, ITEMS.UPC_CODE, ITEMS.MPN,
ITEMS.ITEM_ID, INVENTORY_NUMBER.CLEI, INVENTORY_NUMBER.CERTIFICATION_REF_ID
FROM INVENTORY_NUMBER
INNER JOIN ITEMS ON INVENTORY_NUMBER.ITEM_ID = ITEMS.ITEM_ID
INNER JOIN TRANSACTIONS AS PO ON INVENTORY_NUMBER.PURCHASE_ORDER_ID = PO.TRANSACTION_ID
INNER JOIN TRANSACTIONS AS IR ON INVENTORY_NUMBER.ITEM_RECEIPT_ID = IR.TRANSACTION_ID
INNER JOIN TRANSACTION_LINES ON PO.TRANSACTION_ID = TRANSACTION_LINES.TRANSACTION_ID
INNER JOIN ENTITY ON TRANSACTIONS.ENTITY_ID = ENTITY.ENTITY_ID
INNER JOIN CONDITION ON INVENTORY_NUMBER.CONDITION_ID = CONDITION.LIST_ID
INNER JOIN BRAND_PARTNER ON INVENTORY_NUMBER.BRAND_PARTNER_ID = BRAND_PARTNER.LIST_ID
INNER JOIN PPROGRAM ON INVENTORY_NUMBER.PROGRAM_ID = PPROGRAM.LIST_ID
INNER JOIN INVENTORY_SOURCE ON INVENTORY_NUMBER.INVENTORY_SOURCE_ID = INVENTORY_SOURCE.LIST_ID
INNER JOIN LOCATIONS ON INVENTORY_NUMBER.LOCATION_ID = LOCATIONS.LOCATION_ID
INNER JOIN TEST_RESULTS ON INVENTORY_NUMBER.TEST_RESULTS_ID = TEST_RESULTS.LIST_ID
WHERE INVENTORY_NUMBER.ON_HAND_COUNT IS NOT NULL AND ((INVENTORY_NUMBER.PURCHASE_ORDER_ID IS NULL) OR (INVENTORY_NUMBER.PURCHASE_ORDER_ID IS NOT NULL AND TRANSACTION_LINES.TRANSACTION_LINE_ID IS NOT NULL))
you could also remove the join to the transaction_lines and instead of tl.TRANSACTION_LINE_ID IS NOT NULL use an exists clause
and exists (select 1 from transaction lines tl
where tl.transaction_id = po.transaction_id)
I would suggest using a GROUP BY to help limit your results. You could also if you are interacting with transactions in your query you must always remember that without limiting results based on the "main line" you will receive the header record and then a record for each individual line item.
If you were doing this with a saved search you could put the criteria as "main line = true". Since I don't understand your query entirely I can't advise where to put this limitation in.

Can Derby handle scalar subqueries in SELECT clause?

I'm having trouble getting my query working. Could someone cast an experienced eye on it please? The table structure is simple (2 one-to-many relationships). The query is trying to work out for each sign, how many contributions there are at each unique "PositionLocation".
Sign <- Signifier (f_key sign_oid) <- Contribution (f_key signifier_oid)
I'm getting the following error:
Error: An ON clause associated with a JOIN operator is not valid.
SQLState: 42972
ErrorCode: -1
My query is:
select s.NAME, c.POSITIONLOCATION, count(*) as num_per_locn,
(
select count(*) from APP.CONTRIBUTION c2
inner join APP.SIGNIFIER si2 on si2.OID = c2.SIGNIFIER_OID
inner join APP.SIGN s2 on s2.OID = si2.SIGN_OID
and s2.OID = s.OID
) as num_per_sign
from APP.CONTRIBUTION c
inner join APP.SIGNIFIER si on si.OID = c.SIGNIFIER_OID
inner join APP.SIGN s on s.OID = si.SIGN_OID
group by s.NAME, c.POSITIONLOCATION

correlated sub query

Trying to show customers' vehicles who had an invoice raised in the past 30 days.
I tried this:
select C.*, V.*
from CAR_OWNERSHIP O
join VEHICLE V on v.VEH_ID = O.VEH_ID
join CUSTOMER C on C.CUS_ID = O.CUS_ID
where exists (select null
from INVOICE I
where I.INV_ID = O.INV_ID and
I.INV_DATE >= date() - 30);
Im getting "syntax error in FROM clause"
I have quickly tried a query in access and I get the same error you get but when I change the JOIN to a specific join like LEFT OUTER JOIN or INNER JOIN then that error goes away but it is replaced with another
Syntax error (missing operator) in query expression in
I researched that and found this post which indicates that access requires parentheses when using more than one join
select *
from (CAR_OWNERSHIP O
left outer join CUSTOMER C on C.CUS_ID = O.Cus_ID)
left outer join VEHICLE V on v.VEH_ID = O.VEH_ID
where exists (select null
from INVOICE I
where I.INV_ID = O.INV_ID and
I.INV_DATE >= date() - 30);
I do hope this helps

PostgreSQL delete with inner join

DELETE B.*
FROM m_productprice B
INNER JOIN m_product C ON B.m_product_id = C.m_product_id
WHERE C.upc = '7094' AND B.m_pricelist_version_id = '1000020'
i am getting the following error PostgreSQL 8.2.11
ERROR: syntax error at or near "B"
LINE 1: DELETE B.* from m_productprice B INNER JOIN m_product C ON ...
i tried giving
DELETE B from m_productprice B INNER JOIN m_product C ON B....
ERROR: syntax error at or near "B"
i tried giving
ERROR: syntax error at or near "INNER"
LINE 1: DELETE from m_productprice B INNER JOIN m_product C ON B.m_...
what is the problem with my query?
DELETE
FROM m_productprice B
USING m_product C
WHERE B.m_product_id = C.m_product_id AND
C.upc = '7094' AND
B.m_pricelist_version_id='1000020';
or
DELETE
FROM m_productprice
WHERE m_pricelist_version_id='1000020' AND
m_product_id IN (SELECT m_product_id
FROM m_product
WHERE upc = '7094');
This worked for me:
DELETE from m_productprice
WHERE m_pricelist_version_id='1000020'
AND m_product_id IN (SELECT m_product_id
FROM m_product
WHERE upc = '7094');
If you have more than one join you could use comma separated USING statements:
DELETE
FROM
AAA AS a
USING
BBB AS b,
CCC AS c
WHERE
a.id = b.id
AND a.id = c.id
AND a.uid = 12345
AND c.gid = 's434sd4'
Reference
Another form that works with Postgres 9.1+ is combining a Common Table Expression with the USING statement for the join.
WITH prod AS (select m_product_id, upc from m_product where upc='7094')
DELETE FROM m_productprice B
USING prod C
WHERE B.m_product_id = C.m_product_id
AND B.m_pricelist_version_id = '1000020';
Just use a subquery with INNER JOIN, LEFT JOIN or smth else:
DELETE FROM m_productprice
WHERE m_product_id IN
(
SELECT B.m_product_id
FROM m_productprice B
INNER JOIN m_product C
ON B.m_product_id = C.m_product_id
WHERE C.upc = '7094'
AND B.m_pricelist_version_id = '1000020'
)
to optimize the query,
use NOT EXISTS instead of IN
and WITH for large subqueries
Essentially everything mentioned here is mentioned in the docs, but no-one is specifying exactly what. So this is what the current (v15) DELETE docs says:
Notes
PostgreSQL lets you reference columns of other tables in the WHERE condition by specifying the other tables in the USING clause. For example, to delete all films produced by a given producer, one can do:
DELETE FROM films USING producers
WHERE producer_id = producers.id AND producers.name = 'foo';
What is essentially happening here is a join between films and producers, with all successfully joined films rows being marked for deletion. This syntax is not standard. A more standard way to do it is:
DELETE FROM films
WHERE producer_id IN (SELECT id FROM producers WHERE name = 'foo');
In some cases the join style is easier to write or faster to execute than the sub-select style.

Left Join on Same table - Not recognizing nested SELECT statement

I am trying to pull two different values based on different criteria from the same table and in my Left Join of the same table it is not recognizing the SELECT statement.
The error is as follows:
Dynamic SQL Error
SQL error code = -104
Token unknown - line 7, char -1
SELECT.
The SQL Statement:
SELECT
b.dept,b.typ,c.brand,c.style,c.ext,c.description,
max(c.price),max(c.last_cost),sum(c.quan) "TOTAL INV",D.QUAN "WEB INV"
FROM
invt c
left outer join (
SELECT dept,typ,brand,style,ext,description,sum(quan) as d.quan
FROM invt WHERE store in ('997')
group by dept,typ,brand,style,ext,description) d
on (b.store = d.store and b.style = d.style and b.brand = d.brand)
LEFT OUTER JOIN
sku b
on c.style = b.style and c.brand = b.brand
where c.quan <> 0 or c.ord <> 0
GROUP BY
b.dept,b.typ,c.brand,c.style,c.ext,c.description
Try changing this line:
SELECT dept,typ,brand,style,ext,description,sum(quan) as d.quan
to this:
SELECT store,dept,typ,brand,style,ext,description,sum(quan) as quan
You do not need the d alias here.
UPDATE:
As #Jeremy Holovacs mentioned, you also seem to be using d.store for your join but it does not exist in your subquery.