Update several rows from a select using values from the select query - mysql

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?

Related

MySQL trouble connecting two columns of names

I have two tables that I'm trying to connect. One table is called 2019projections and the other is called 2019actualstat. I want to connect the two by names. I'm 99% sure every name that is in 2019actualstat is in 2019projections, but not every name in 2019actualstat is in 2019projections. The latter has alot more names, but most of them are useless.
I've tried left join and right join.
I've tried select distinct
I gave a shot at exists
This is what I have so far:
USE Fantasyfootball;
SELECT DISTINCT *
FROM 2019actualstat;
LEFT JOIN 2019projections ON
2019actualstat.Player =
2019projections.first_last;
It's giving me the 1064 error, but I think it has to do with the 2019projections table having more records.
21:27:26 LEFT JOIN 2019projections ON 2019actualstat.Player =
2019projections.first_last Error Code: 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 'LEFT JOIN 2019projections ON
2019actualstat.Player = 2019projections.first_last' at line 1 0.00071
sec
2019projections.first_last is a varchar(50) and 2019actualstat.player is text
PS: I have the .csv files which I'm not sure how to post, but I would be happy to send them both.
You're missing the select list, and have a redundant (wrong) semicolon at the end of the from clause:
SELECT *
FROM 2019actualstat
LEFT JOIN 2019projections ON 2019actualstat.Player = 2019projections.first_last;

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

How to resolve wrong syntax near HAVING COUNT DISTINCT?

This it the code I am trying to execute:
SELECT ID_K
FROM koncert,
programi
WHERE koncert.ID_K = programi.ID_K
GROUP BY koncert.ID_K
HAVING COUNT (DISTINCT programi.Salla) = 2
It returns this error:
#1064 - 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 'DISTINCT programi.Salla)=2 LIMIT 0, 25' at line 4.
Tried to change different things but it still won't work .
You should use the count(DISTINCT programi.Salla
) and not count (..) ..remove space between COUNT and (...
SELECT koncert.ID_K
FROM koncert
INNER JOIN programi on koncert.ID_K = programi.ID_K
GROUP BY koncert.ID_K
HAVING COUNT(DISTINCT programi.Salla) = 2
but you need also tablename for avoid ambiguity and use explicit join sintax too
First you should use qualified name for your column, when column name is same in both table
SELECT ID_K FROM
should be
SELECT programi.ID_K FROM
else, you will get ambiguous column error. Otherwise, your query looks fine except removing extra space when calling COUNT (#spencer already mentioned in comment)
Also, it is good practice to join your table using JOIN (or INNER JOIN, LEFT JOIN etc) keyword, which makes your query more clear and readable.

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.