SQL Error in Syntax near except - mysql

I'm trying to select persons who have an account at all branches of the city.
(With a SQL query)
SELECT A.customId
FROM accountholder as A
WHERE NOT EXISTS (
(SELECT name
FROM branch
WHERE city='LA')
EXCEPT (SELECT C.branch
FROM accountholder AS B, account AS C
WHERE B.accountnumber = C.accountnumber
AND A.customId = B.customId));
Now I got:
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 'EXCEPT (SELECT C.branch FROM accountholder AS B, account AS C WHERE B.accountnumber=' at line 1
And I do not see the problem. Am I blind or just stupid?
Thanks for help.

MySQL does not use EXCEPT. Use NOT IN.
SELECT A.customId
FROM accountholder as A
WHERE branch NOT IN (
(SELECT name FROM branch WHERE city='LA')
AND branch NOT IN (SELECT C.branch FROM accountholder AS B, account AS C WHERE B.accountnumber = C.accountnumber AND A.customId = B.customId));

As others have stated, MySQL does not support the EXCEPT operation. You will have to rewrite your query.
You appear to be querying accountholders if there is no other branch in LA besides those branches at which the accountholder's accounts are held.
Here's a rough guess:
SELECT A.customId
FROM accountholder AS A
JOIN account AS C
ON A.accountnumber = C.accountnumber
LEFT OUTER JOIN branch AS B
ON C.branch <> B.name AND B.city = 'LA'
WHERE B.city IS NULL;
I'm making some assumptions about your tables and columns and their relationships, so this query is just a guess. Do not just run it blindly and expect it to work. I ask you to use it as an example and confirm that the comparisons are being done correctly for your data.

MySQL only supports UNION but not INTERSECT and EXCEPT/MINUS
Adding these set operations has been a long standing feature request
http://bugs.mysql.com/bug.php?id=1309
You may want to vote "Affects me" on that bug report ...

Related

MySQL Basic query

I have managed to get a result that says full_name and facility_name.
Here the facility names consists of "Tennis Court1","Tennis Court2","Squash Court", "Badminton" and "Table Tennis"
I now want to filter and present by only "Tennis Court"
I have tried adding
WHERE facility_name ILIKE '%Tennis Court%
but get an error that says:
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 'ILIKE '%Tennis%' at line 15
SELECT
DISTINCT(CONCAT(z.firstname,' ', z.surname)) AS full_name,
facilities.name AS facility_name
FROM
(SELECT members.memid,
bookings.facid,
members.firstname,
members.surname
FROM Members AS members
RIGHT JOIN Bookings AS bookings
ON members.memid =bookings.memid) z
RIGHT JOIN Facilities as facilities
ON facilities.facid = z.facid
WHERE facility_name LIKE 'Tennis Court%'
Here are the links to the table.
TABLES used in the SQL query
The error : #1054 - Unknown column 'facility_name' in 'where clause'. Just to inform you. There are 2 facility names 'Tennis Court1' and 'Tennis Court2'
Replace ILIKE TO LIKE in your query and remove brackets after distinct as distinct is not a function
Incidentally, your query appears to be functionally identical to:
SELECT DISTINCT CONCAT_WS(' ',m.firstname,m.surname) full_name
, f.name facility_name
FROM facilities f
JOIN bookings b
ON b.facid = f.facid
LEFT
JOIN members m
ON m.memid = b.memid
WHERE f.name LIKE 'Tennis Court%'
And, if I'm wrong, see Why should I provide an MCRE for what seems to me to be a very simple SQL query
Thank you very much
I used facid instead of facility_name
The code goes as follows:
SELECT
DISTINCT CONCAT(z.firstname,' ', z.surname) AS full_name,
facilities.name as facility_name
FROM
(SELECT members.memid,
bookings.facid,
members.firstname,
members.surname
FROM Members AS members
RIGHT JOIN Bookings AS bookings
ON members.memid =bookings.memid) z
RIGHT JOIN Facilities as facilities
ON facilities.facid = z.facid
WHERE facilities.facid=0 OR facilities.facid=1

Intersect command is not working

MariaDB [object]> select Protein, count(mirna) from exp2
INTERSECT select Protein, count(mirna) from exp3 group by Protein;
ERROR 1064 (42000): 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 'select Protein, count(mirna) from exp3 group by
Protein' at line 1.
I have two tables exp2 and exp3, both have many common rows, I want to query from the common data from these two tables.i.e. I want have a common data table of Protein and corresponding count of miRNAs in number.
I am using lampp, how I can resolve this query?
MySQL doesn't offer the INTERSECT operation. You need to use something like a JOIN operation. This example suppresses all rows that don't match the ON condition.
SELECT a.Protein, a.mirnacount
FROM (SELECT Protein, count(mirna) mirnacount from exp2 group by Protein) a
JOIN (SELECT Protein, count(mirna) mirnacount from exp3 group by Protein) b
ON a.Protein = b.Protein AND a.mirnacount = b.mirnacount
You might consider switching to PostgreSQL if a full complement of set operations are needed for your project.
SELECT T1.Protein,count(T1.miRNAID)
FROM exp2 AS T1
INNER JOIN exp3 AS T2
ON T1.Protein = T2.Protein
AND T1.Target_Protein_id= T2.Target_Protein_id
AND T1.miRNAID=T2.miRNAID
GROUP BY T1.Protein
ORDER BY count(T1.miRNAID)
this works perfectly for me.

SQL query to retrieve data from 3 tables

I have branch table , inventory table and item table. I want to retrieve item id, item name , qty (quantity) and branch name (branch_add).
SELECT tbl_item.item_name ,
tbl_inventory. tbl_item_item_ID , tbl_inventory.qty , tbl_branch.branch_add
FROM tbl_item,tbl_inventory ,tbl_branch
WHERE (tbl_inventory.tbl_item_item_ID = tbl_item.item_ID) JOIN (tbl_branch.branch_ID=tbl_inventory`.tbl_branch_branch_ID)
That is the code which I have wrote but it gives bellow 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 'join item_ID.tbl_item =
tbl_item_item_ID.tbl_inventory' at line 3
Could anyone please help me to solve this. And table will displayed on attached png.
You have a missing AND between your WHERE conditions:
select
tbl_branch_branch_ID.tbl_inventory
, item_name
from tbl_inventory
, tbl_item
, tbl_branch
where
branch_ID.tbl_branch=tbl_branch_branch_ID.tbl_i
and item_ID.tbl_item = tbl_item_item_ID.tbl_inventory;
Also, your JOIN conditions are a little bit off.
I think this is the query you're actually looking for:
select
t_inv.*
, t_itm.item_name
from tbl_inventory t_inv
inner join tbl_item t_itm on t_inv.t_item_item_id = t_itm.item_id
inner join tbl_branch t_br on t_br.branch_id = t_inv.tbl_branch_branch_id
Using explicit JOINs is much better and clearer for reading the code and also can result in improved code performance.
SELECT
tbl_item.item_name,
tbl_inventory.tbl_item_item_ID,
tbl_inventory.qty,
tbl_branch.branch_add
FROM
tbl_item,
tbl_inventory,
tbl_branch
WHERE
(
inner join tbl_item ON tbl_inventory.tbl_item_item_ID = tbl_item.item_ID
inner join branch_ID ON tbl_branch.branch_ID = tbl_inventory.tbl_branch_branch_ID
);
The above query gives syntax error.

Syntax error in MySQL Join Query

I'm getting a syntax error in MySQL query. Is MySQL and SQL server work differently? Can anyone suggest, what is wrong and where ?
select b.component, d.matter, d.bug, d.timestamp, d.os
from bugs.profiles p, ops_reports.BPR_TAG_DATA d
left join (Select * from bugs where product='test') b
on d.bug=b.bug_id
where d.tagid = 6
and timestamp between "2014-04-21" and "2014-04-24"
and login_name like 'test'
and p.userid = d.user
Error Message 24/04/2014 23:14:10 0:00:00.037 MySQL Database 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 'Select * from bugs where product='Conversions') as b
on (d.bu 1 0
You should not mix implicit and explicit joins. A simple rule: just don't use commas in the from clause.
select b.component, d.matter, d.bug, d.timestamp, d.os
from ops_reports.BPR_TAG_DATA d left join
bugs b
on b.product = 'test' and d.bug = b.bug_id left join
bugs.profiles p
on p.userid = d.user
where d.tagid = 6 and
timestamp between '2014-04-21' and '2014-04-24' and
login_name like 'test';
I also removed the subquery, moving the condition to the on clause. This makes the query more efficient. And changed the delimiters for the date constants to single quotes. Using double quotes for strings can lead to confusion.
EDIT:
All this said, the query in the question looks like it is syntactically correct. I notice that the error message does not refer to this exact query. The query has product='test') b and the error message has product='Conversions') as b. Perhaps there are other differences as well.

Can I count number of rows in joined table?

Like this:
SELECT s.*, count( logs.* ) as ssh_count
FROM servers s
LEFT JOIN logs ON s.ip_address = logs.server_ip
But I get an error with that query:
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 ssh_count FROM servers s LEFT JOIN logs ON s.ip_address = logs.server_ip LIMIT' at line 1
I think that's because you can't address a table in the count function.
I can do this using a subquery, but that will likely slowly down the query.
What is a better way of doing this?
You can adress a table column, but you can't address table.*, you can do this for example:
SELECT s.*, count( logs.server_ip ) as ssh_count
FROM servers s
LEFT JOIN logs ON s.ip_address = logs.server_ip