Why does this query break when I include a second WHERE condition? - mysql

The query is
SELECT account_id, type_id, client_id, avail_balance
FROM accounts
WHERE open_emp_id
IN (SELECT emp_id
FROM employee
WHERE (branch_id = 1111) AND (job_id NOT LIKE ‘HD%’);
Without the second WHERE condition, the query works fine. With that condition, I get
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 '%’))' at line 1

Related

Create a SQL view with conditions

I wanted to create a view that selected animal_id, date from service_records table with conditions of Canine type and bath service, but I got an error message:
#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 ' service_type='B'.
How should I fix it?
Here is my code:
CREATE VIEW dogs_bath_records
AS
SELECT animal_id, date
FROM service_records
WHERE animal_type = 'Canine', service_type = 'B';
Use AND for multiple conditions
CREATE VIEW dogs_bath_records AS
SELECT animal_id, date
FROM service_records
WHERE animal_type='Canine'
AND service_type='B';

last query result

SELECT sma_quotes.customer as name,
sma_quotes.date as date,
sma_quotes.selecttype as type,
sma_quotes.biller_id as bl_id,
sma_quotes.volume as volume,
sma_quotes.containernumber as cn_no,
sma_quotes.grand_total as total,
sma_sales.paid as paid
FROM sma_quotes
JOIN sma_sales ON sma_sales.quote_id = sma_quotes.id
WHERE name IS 'Everbest Foods'
Error
SQL query: Documentation
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 ''Everbest Foods' LIMIT 0, 25' at line 1
There is no LIMIT in your query, so the error message is suspicious.
However, you want =, not IS:
WHERE sma_quotes.customer = 'Everbest Foods'

what is wrong following mysql query?

INSERT INTO lm_empleavetypemodelmap(leavetypeid,userid)
VALUES(1682,"b0c6c81f-a20a-4daa-9038-831478d8e11b")
WHERE lm_empleavetypemodelmap.userid NOT IN
(SELECT lm_empleavetypemodelmap.userid
FROM lm_empleavetypemodelmap
WHERE lm_empleavetypemodelmap.leavetypeid = 1683)
[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 'WHERE lm_empleavetypemodelmap.userid NOT IN( select lm_empleavetypemodelmap.user' at line 1
You do not use where with values. Perhaps the stuff at the end is just accidental garbage and you just want:
INSERT INTO lm_empleavetypemodelmap(leavetypeid, userid)
SELECT 1682, 'b0c6c81f-a20a-4daa-9038-831478d8e11b';
Or, in what looks rather strange to me:
INSERT INTO lm_empleavetypemodelmap(leavetypeid, userid)
SELECT 1682, 'b0c6c81f-a20a-4daa-9038-831478d8e11b'
WHERE NOT EXISTS (SELECT 1 FROM lm_empleavetypemodelmap WHERE leavetypeid = 1683)
That is, insert 1682, if 1683 doesn't exist. Usually you care about the value being inserted, not the next value.

select info from dbs and delete (not working)

I have this query:
DELETE FROM amx_admins_servers, amx_amxadmins
WHERE
amx_admins_servers.admin_id = (SELECT id FROM amx_amxadmins WHERE username='kokoz')
AND amx_amxadmins.username = 'kokoz'
but didnt work.
I get sql 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 'WHERE amx_admins_servers.admin_id = (SELECT id FROM
amx_amxadmins WHERE username' at line 2
where is the problem ?
Looks like you want to delete an admin user and all its entries in the server table. Use
DELETE a, s
FROM amx_amxadmins a
inner join amx_admins_servers s on a.id = s.admin_id
WHERE a.username='kokoz'

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;