CodeIgniter Concat - mysql

I've spent a few hours staring at this piece of code. Fresh eyes please!
Here is a shortened version of the 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
'FROM (`requests` c) JOIN `inventory` d ON `d`.`listing_seq_no` = `c' at line 7
SELECT DISTINCT `c`.`req_id`, `u`.`user_id`, `u`.`org_name`,
CONCAT_WS(' ', `l`.`strength`, `l`.`unit)` as dos, `c`.`quantity` AS quantity1,
(SELECT sum(quantity) from inventory d2
WHERE d2.listing_seq_no = c.listing_seq_no
) as inv_total,
FROM (`requests` c)
JOIN `inventory` d
ON `d`.`listing_seq_no` = `c`.`listing_seq_no`
JOIN `listings` l
ON `l`.`listing_seq_no` = `c`.`listing_seq_no`
EDIT: Original CodeIgniter Code snippet:
$this->db->select ( "c.req_id,
u.user_id,
u.org_name,
l.tradename as name,
CONCAT_WS(' ', l.strength, l.unit) as dos,
);

This:
CONCAT_WS(' ', `l`.`strength`, `l`.`unit)`
Should be:
CONCAT_WS(' ', `l`.`strength`, `l`.`unit`)

try removing the parens around (requests c)

It's an old question, but you can use:
$this->db->select("<your_select_portion>", FALSE);
to avoid the auto-quoting feature of the CI DB class. I am a fan of using MySQL functions on select statements with CI, and this usually fixes the problem (except when you start using the query-caching feature, but that's another story).

Related

Error after migrating from Mysql 5.7 to MariaDb 10.6.5

I am using PHP-MySQLi-Database-Class to construct the queries, it is working fine on MySQL but after switching to a MariaDB database a single query in specific is throwing this error: (sorry for the long text)
*Uncaught Exception: 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 'JSON) FROM attributes AS _sub_attr WHERE JSON_EXTRACT(sub_prop_attr.attrib_te...' at line 1 query: SELECT ,p.id AS id, DATEDIFF(CURDATE(),DATE(p.created)) AS days, COALESCE(bed_room,bath_room,parking,construction_size,lot_size,floors,building_age) AS chs , (SELECT JSON_OBJECTAGG(attribute_name, (SELECT CAST(CONCAT('[',GROUP_CONCAT(JSON_QUOTE(_sub_attr.attribute_name)),']') AS JSON) FROM attributes AS _sub_attr WHERE JSON_EXTRACT(sub_prop_attr.attrib_terms, JSON_UNQUOTE(JSON_SEARCH(sub_prop_attr.attrib_terms, 'one', _sub_attr.id))) IS NOT NULL)) FROM prop_attributes AS sub_prop_attr LEFT JOIN attributes AS sub_attr ON sub_prop_attr.attribute_id = sub_attr.id WHERE prop_id = p.id) AS JSON_attributes FROM properties p LEFT JOIN (SELECT id, state_name, CONCAT("en ",state_name) AS state_alias FROM states) s on p.state=s.id LEFT JOIN (SELECT id, city_na in C:\wamp64...\classes\MysqliDb.php on line 1819
...And this is the query:
SELECT *,p.id AS id, DATEDIFF(CURDATE(),DATE(p.created)) AS days,
COALESCE(bed_room,bath_room,parking,construction_size,lot_size,floors,building_age) AS chs ,
(SELECT JSON_OBJECTAGG(attribute_name,
(SELECT CAST(CONCAT(\'[\',GROUP_CONCAT(JSON_QUOTE(_sub_attr.attribute_name)),\']\') AS JSON)
FROM attributes AS _sub_attr
WHERE JSON_EXTRACT(sub_prop_attr.attrib_terms, JSON_UNQUOTE(JSON_SEARCH(sub_prop_attr.attrib_terms, \'one\', _sub_attr.id))) IS NOT NULL))
FROM prop_attributes AS sub_prop_attr LEFT JOIN attr';
This is the MySQL query:
$Srelated_attributes = ", (SELECT JSON_OBJECTAGG(attribute_name, (SELECT CAST(CONCAT('[',GROUP_CONCAT(JSON_QUOTE(_sub_attr.attribute_name)),']') AS JSON) FROM attributes AS _sub_attr WHERE JSON_EXTRACT(sub_prop_attr.attrib_terms, JSON_UNQUOTE(JSON_SEARCH(sub_prop_attr.attrib_terms, 'one', _sub_attr.id))) IS NOT NULL)) FROM prop_attributes AS sub_prop_attr LEFT JOIN attributes AS sub_attr ON sub_prop_attr.attribute_id = sub_attr.id WHERE prop_id = p.id) AS JSON_attributes";
The last is part of a long query and if I take out that part it would work... I think it may be related to quotes or maybe JSON functions but I haven't find any nice solution to make that same code work.
Do any of you have had a related experience? Hope I was clear with my problem description.
I expect I can use my query. Also I've tried erasing the part of the query which I quoted in the issue description and the error disappears so I the problem may be arround that part of the query.
Because JSON isn't a type in the SQL standard, MariaDB hasn't implemented the CAST(... AS JSON) as its not a type.
If you remove those words and the CAST operator will behave in the same way.

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

Nested SQL in mySQL

I am getting below error when I have below SQL for mySQL. Can someone please help me what am I doing wrong. OR Is there any better way to achieive same ?
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 '(SELECT role FROM temp_scrconflict WHERE release = 'GROUP6')
= ANY (SELECT ' at line 2
Select * from `temp_scrconflict`
WHERE ANY (SELECT role
FROM `temp_scrconflict`
WHERE `release` = 'GROUP6') = ANY (SELECT role
FROM `temp_scrconflict`
WHERE `release`
IN ('ER_JUNE15', 'ER_APR15'))
I've never used it myself, but according to the official documentation:
"The ANY keyword [...] must follow a comparison operator"
Error is in below pointed line. See Documentation
Select * from `temp_scrconflict`
WHERE ANY (SELECT role <-- Here
You need to use a column name along with a comparison operator like
where some_column > ANY (subquery)
You can modify your query to be like below using IN operator
Select * from `temp_scrconflict`
WHERE (SELECT distinct role
FROM `temp_scrconflict` WHERE `release` = 'GROUP6') IN (SELECT role
FROM `temp_scrconflict` WHERE `release` IN ('ER_JUNE15', 'ER_APR15'));

mySQL Error near AS

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.

Inner join with like clause

I am using inner join with the like clause ..
My tried sql is
SELECT tbl_songs.id AS sid,
tbl_songs.name AS sname,
tbl_albums.id AS aid,
tbl_albums.name AS aname
FROM tbl_songs
INNER JOIN tbl_albums
ON tbl_songs.albums LIKE '%' + tbl_albums.name + '%';
Its showing me syntax 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 '+ tbl_albums.name + '%'' at line 2
Please elaborate reason of syntax error.
you have to form the clause using concat ...
...LIKE CONCAT('%',tbl_albums.name, '%');
there is no + operator like this in mysql
You can use below format in oracle sql:
SELECT tbl_songs.id AS sid,
tbl_songs.name AS sname,
tbl_albums.id AS aid,
tbl_albums.name AS aname
FROM tbl_songs
INNER JOIN tbl_albums
ON tbl_songs.albums LIKE ('%'||tbl_albums.name||'%');
An example of MySQL:
SELECT tbl_songs.bus_name FROM tbl_songs , tbl_albums
WHERE tbl_songs.albums LIKE CONCAT('%',tbl_albums.name, '%');