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
Related
So my assignment question is to "Get a list of all books withdrawn by people with the initials 'B.W.'. Show a column for the first name, last name, initials, and the title of the book". I am trying to join these 3 tables as it is the only way to get this information, but Im having a hard time only displaying names with initials B.W. I get a syntax error saying:
"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 withdrawals
When I remove my 'WHERE' statement (all of the second line), my syntax error goes away. How do I make this work?
Below is my code:
SELECT first_name, last_name, title FROM members
WHERE first_name LIKE 'B%' AND last_name LIKE 'W%'
JOIN withdrawals
ON members.member_id = withdrawals.member_id
JOIN books
ON withdrawals.book_id = books.book_id
JOIN should always be placed just after FROM table-name. The below should work
SELECT first_name, last_name, title FROM members
JOIN withdrawals
ON members.member_id = withdrawals.member_id
JOIN books
ON withdrawals.book_id = books.book_id
WHERE first_name LIKE 'B%' AND last_name LIKE 'W%'
There is a flow you have to follow when writing your SQL statements
SELECT * FROM table
JOIN table2 ON
WHERE
GROUP BY
First, you will need to create a column called Initials. In this case, you can use SUBSTR to get the first letter from you first_name and the first letter from last_name. Then, you will need to CONCAT them together to make it a column.
CONCAT(SUBSTR(first_name,1,1), '.', SUBSTR(last_name,1,1), '.') AS Initials
After that, you need to find out who have the initials with B.W. . You can use LIKE ('B.W.'). Since you have a specific answer, you do not need to use %.
HAVING Initials LIKE ('B.W.')
Try to follow everything in order which goes like:
SELECT - FROM - JOIN & ON - WHERE - GROUP BY - HAVING - ORDER BY
You can try this:
SELECT CONCAT(SUBSTR(m.first_name,1,1) , '.', SUBSTR(m.last_name,1,1) ) AS "Initials"
FROM - JOIN - ON
WHERE m.first_name LIKE "B%" AND m.last_name LIKE "W%" ;
I try something like this:
UPDATE features f2
SET f2.description = REPLACE(f2.description,f.name,CONCAT("<span
class='condition'>",f.name,"<input type='hidden' class='id' value='",f.id,"' /></span>"))
FROM features f2
RIGHT JOIN features f
ON (LOCATE(concat(" ",f.name),f2.description)>0)
WHERE f.id_ft=2 AND f2.id_ft=12
But it is not working.
#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 features f2 RIGHT JOIN features f ON (LOCATE(concat(" ",f.name),f2.desc' at line 3
The idea is replacing some text within a field from features table with another field text from the same table (but different row). The select That works for me is the following:
SELECT f.id, f.name, f2.name, f2.id FROM `features` f2 RIGHT JOIN features f ON (LOCATE(concat(' ',f.name),f2.description)>0) WHERE f.id_ft=2 AND f2.id_ft=12 order by f.name, f2.name
Any idea?
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.
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 ...
Can anyone help with this? I've just switched web servers and I'm testing everything is working but I'm seeing this error. Any ideas whats wrong with the query? It seemed to be valid on my last host
Critical Error
A database error has occoured.
Error Returned mySQL query error: SELECT f.* AS fixtures,
team1.teamName AS HomeTeam,
team1.tid AS HomeTeamID,
team2.teamName AS AwayTeam,
team2.tid AS AwayTeamID,
GROUP_CONCAT(n.extra separator ',') AS scorers,
GROUP_CONCAT(n.homeEvent separator ',') AS homeEvent,
GROUP_CONCAT(n.eventType separator ',') AS eventType
FROM fixtures f
LEFT JOIN notifications n ON n.fixtureID = f.fid
LEFT JOIN teams team1 ON team1.tid = f.HomeTeam
LEFT JOIN teams team2 ON team2.tid = f.AwayTeam
WHERE f.kickoff > 1403823600 AND f.lid=1
GROUP BY f.fid
ORDER BY n.time ASC, f.kickoff ASC
mySQL 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 'AS fixtures,
team1.teamName AS HomeTeam,
team1.tid AS HomeTeamID,
' at line 1
You can't cast a wildcard like that.. Casting is only for single fields
SELECT f.* AS fixtures
Try something like
SELECT f.fixtures AS fixtures, f.field AS field
etc
I don't know what kind of sql engine did you run on your previous webserver, but this is actually not allowed:
SELECT f.* AS fixtures
You need to specify a column, you can't use the wildcard for casting.