UPDATE product
INNER JOIN erpproduct on erpproduct.ProductId = product.ProductId
SET
product.ProductTypeId = (SELECT productTypeId FROM productType WHERE producttype.producttypeName = 'MyProductTypeName' LIMIT 1 ),
WHERE erpproduct.ErpProductId = 123123123;
i got an 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 producttype ..."
MySQL version is 4.0.27, it seems that MySQL doesn't allow Select to specify a SET value. Without SELECT everything goes just fine. Is this the case or am I doing something wrong in the statement ?
This extremly old version of MySQL doesn't support subselects. You need MySQL v4.1 as a minimum.
I quote from the relevant part of the manual of those old versions:
With MySQL versions prior to 4.1, it was necessary to work around or
avoid the use of subqueries. In many cases, subqueries can
successfully be rewritten using joins and other methods. See Section
12.2.8.11, “Rewriting Subqueries as Joins for Earlier MySQL Versions”.
Related
I am trying to execute a query into MySQL but it keeps telling me i am using the wrong syntax, I tried searching the MySQL community but I am not getting anything usefull.. most of the answers i find on google are for other databases yet they label them for "MySQL", yet it keeps failing.
This is the statement i am trying to execute:
$statement = "IF (SELECT ttb_id FROM timetable WHERE ttb_week = $i AND ttb_time = $j) THEN
BEGIN
UPDATE types SET typ_name = '$subj'
WHERE typ_name = 'student';
END;
ELSE
BEGIN
INSERT INTO types VALUES (null,`Yo`);
END;
";
error:
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 'IF (SELECT ttb_id FROM timetable WHERE ttb_week = 0 AND ttb_time = 0) THEN ' at line 1
I am using:
PHP Script Language Version 5.2.6
MySQL Database Version 5.0.51b
I have been looking around but to no avail, and the if condition stated on the MySQL dev website are not helping at all..
I am new to this and it is driving me mad! all the different queries i tried failed.. it is never the right syntax.
I found many answers for the problem on stackExchange and other websites but it is always wrong.. also I remember this structure from a VB.net lesson a while ago so maybe this is for MSSQL? then what about MySQL? everyone so far listed similar structure and said it works for MySQL, I took my answer from an answer on this community* labeled for MySQL and he claimed it worked. this is one of many i tried.
I would really appreciate your help
*: Usage of MySQL's "IF EXISTS"
My first thought would be you're not comparing your SELECT return to anything to actually utilize your conditional logic. Are you just looking to see if your query returns values? If it doesn't return a value then you insert a new record in otherwise you update.
Maybe use IS NOT NULL or a check to see count on the select to see how many rows and compare to see if that's greater than 0.
IF (SELECT ttb_id FROM timetable WHERE ttb_week = $i AND ttb_time = $j IS NOT NULL) THEN
IF (SELECT COUNT(*) FROM timetable WHERE ttb_week = $i AND ttb_time = $j) > 0 THEN
I have 5.1 MySQL version on my server. I am trying to perform this query:
SELECT File_Name
FROM Words_DB
WHERE Word_Name=" . $element . "
EXCEPT
SELECT File_Name
FROM Files_DB
WHERE Display=0
I am getting an error:
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 'EXCEPT SELECT File_Name FROM Files_DB WHERE Display=0' at line 4
Can someone tell me how can i perform this query in an alternative form?
Thank you, Max.
As far as I know MySQL does not support theEXCEPToperator. Try this instead:
SELECT File_Name
FROM Words_DB
WHERE Word_Name=" . $element . "
AND File_Name NOT IN (
SELECT File_Name
FROM Files_DB
WHERE Display=0
)
You could also use either a correlatedNOT EXISTSor aLEFT JOIN. As I don't use MySQL much I can't say which performs best.
I think you can find better answers on the following site:
http://www.tutorialspoint.com/sql/sql-except-clause.htm
It says you can use except query. But you can also use answer provided by JPW above that instead of using except you can use NOT IN key word which works in the same way.
I'm auditing a project and I found a way to inject data in a query.
The project uses Hibernate and for this piece of code Session.createSqlQuery() and then a .list()
The SQL is something like : "SELECT * FROM tablename ORDER BY column XXXXXX"
XXXXXX can be modified using Fiddler. So I tried
SELECT * FROM tablename ORDER BY column DESC; truncate table tablename;
Unfortunately (well only for my injection attempt) it's not working and I'm getting :
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 'truncate table tablename'
My question is, since they're using createSQLQuery, are they safe from injection. If they're not, could you give me an example to highlight the issue.
I tried using %08 (Backspace character) thinking I would be able to delete previous query characters for example (It didn't work ;) )
Thanks.
After some research it seems I won't be able to modify data with this security hole, however using ORDER BY (CASE WHEN ...) would allow to "scan" the tables and the data.
Is the column name specified using a parameterized statement or are you just concatenating text?
ex: in perl::DBI, the drivers support the following syntax:
$dbh->do("SELECt * FROM asdf ORDER BY ?", undef, $order_by);
The ? there is a form of parameterized statement which sanitizes the input automatically.
I'm running multiple queries on both MySQL and SQLServer (same queries on both servers, same db). Almost all of them run fine. I have a problem with this one:
SELECT
`Extent1`.`IdGosc`,
`Extent2`.`Imie`,
`Extent2`.`Nazwisko`
FROM `TGosc` AS `Extent1`
INNER JOIN `TOsoba` AS `Extent2` ON `Extent1`.`IdGosc` = `Extent2`.`IdOsoba`
WHERE EXISTS(
SELECT 1 AS `C1`
FROM (
SELECT `Extent3`.`IdRezerwacja`
FROM `TRezerwacja` AS `Extent3`
(here!) WHERE `Extent1`.`IdGosc` = `Extent3`.`IdGosc`) AS `Project1`
)
It runs on SQL Server just fine, returns correct results, but MySQL says:
Error Code: 1054. Unknown column 'Extent1.IdGosc' in 'where clause'.
Why so? :|
Are there any limitations about MySQL nested queries?
(Please don't offer queries that return the same and work, I can do that as well, but it's not my point)
I have seen this problem on MySQL.
SELECT `Extent1`.`IdGosc`, `Extent2`.`Imie`, `Extent2`.`Nazwisko`
FROM `TGosc` `Extent1` INNER JOIN
`TOsoba` `Extent2`
ON `Extent1`.`IdGosc` = `Extent2`.`IdOsoba`
WHERE EXISTS (SELECT `Extent3`.`IdRezerwacja`
FROM `TRezerwacja` AS `Extent3`
(here!) WHERE `Extent1`.`IdGosc` = `Extent3`.`IdGosc`
)
Fortunately, in this case, you can just eliminate the middle subquery.
I too have faced this sort of error in mysql.What I have done at tht tym is :
mysql remember only current table so try to do it may b it would work
replace
FROM `TRezerwacja` AS `Extent3
with
FROM `TRezerwacja` AS `Extent3`,`TGosc` AS `Extent1`
Ok. It turns out to be the problem with MySQL.
I'm using Entity Framework's query, that later turns into db specific SQL. I this case MySQL. So, the query in EF is:
var query3a = from TGosc gosc in context.TGosc
where gosc.TRezerwacja
.Any(x => x.TPlatnosc
.Any(y => y.Kwota > 100000))
select new { gosc.IdGosc, gosc.TOsoba.Imie, gosc.TOsoba.Nazwisko };
Now, the provider in my app is Connector NET 6.7.4. It includes MySQL.Data and MySQL.Data.Entities, both in version 6.7.4.
However, I also installed MySQL for Visual Studio 1.0.2 to be able to use more GUI than code in Visual Studio. But this thing comes with same dlls, just in different (older) versions 6.6.5. And these took precedence over the newer ones when application was running. (It's weird in the first place that in the same MySQL Installer there are two somehow conflicting versions of the same dlls.)
Anyway, I removed MySQL for Visual Studio 1.0.2, which left me with the newer dlls and see what happens to the very same LINQ to Entities query, when it's being translated to db sql:
--old 6.6.5
SELECT
Extent1.IdGosc,
Extent2.Imie,
Extent2.Nazwisko
FROM TGosc AS Extent1
INNER JOIN TOsoba AS Extent2 ON Extent1.IdGosc = Extent2.IdOsoba
WHERE EXISTS(
SELECT 1 AS C1
FROM (
SELECT Extent3.IdRezerwacja
FROM TRezerwacja AS Extent3
WHERE Extent1.IdGosc = Extent3.IdGosc) AS Project1
WHERE EXISTS(
SELECT 1 AS C1
FROM TPlatnosc AS Extent4
WHERE (Project1.IdRezerwacja = Extent4.IdRezerwacja)
AND (Extent4.Kwota > 100000)))
vs
-- new 6.7.4
SELECT
Extent1.IdGosc,
Extent2.Imie,
Extent2.Nazwisko
FROM TGosc AS Extent1
INNER JOIN TOsoba AS Extent2 ON Extent1.IdGosc = Extent2.IdOsoba
WHERE EXISTS(
SELECT 1 AS C1
FROM TRezerwacja AS Project1
WHERE EXISTS(
SELECT 1 AS C1
FROM TPlatnosc AS Extent4
WHERE (Project1.IdRezerwacja = Extent4.IdRezerwacja)
AND (Extent4.Kwota > 100000))
AND Extent1.IdGosc = Project1.IdGosc)
It's similar to what Gordon Linoff answered in this post. The middle subquery dissapears.
And of course the new query works fine!
Summing up, I guess the MySQL provider for .NET got better over these versions. I still have some queries that cause similar problems but now I think I know why that is - provider. I'm ok with that.
The annoying thing is that there are two different versions of dlls, one overriding another, in MySQL Installer. I'm using mysql-installer-community-5.6.13.0.
I'm trying to perform a bulk delete of an object, Feature, which has a birdirectional ManyToOne relationship with another class, FeaturesMetadata. I'm having a SQLGrammerException thrown.
The hql I'm using:
String hql = "delete from Feature F where F.featuresMetadata.stateGeoId = :stateGeoId";
Turning on show SQL, the following is generated:
delete from FEATURE cross join FEATURESMETADATA featuresme1_ where STATEGEOID=?
Running the SQL directly in the db client gives this exception:
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 'cross join FEATURESMETADATA featuresme1_ where stategeoid='01'' at line 1
Since the generated SQL is throwing the Exception, I tried changing dialects from MySQL5InnoDBDialect to MySQLInnoDBDialect, but no change.
Can anyone assist?
You may not have joins in such a HQL query. Quote from the reference documentation:
No joins, either implicit or explicit, can be specified in a bulk HQL
query. Sub-queries can be used in the where-clause, where the
subqueries themselves may contain joins.
So I guess something like this should work:
delete from Feature F where F.id in
(select f2.id from Feature f2 where f2.featuresMetadata.stateGeoId = :stateGeoId)
I had the same issue and struggled to find a sensible answer. It seems that, even if you get this approach to work, the SQL generated is highly inefficient (according to what I have read).
So I took a step back and did the following:
List<Properties> props = propDao.findPropertiesByHotelCode(hotel.getCode());
propDao.deleteInBatch(props);
propDao.flush();
Basically rather tan trying to 'delete where', I'm doing a select where and then deleting in batch the set that I retrieved.
Hope this helps.
This is indeed rather poor from Hibernate. But you can solve it like this in a repo: (at least in PostgreSQL, not sure if this syntax should be modified for MySql)
#Modifying
#Query(nativeQuery = true, value = """
DELETE FROM feature f
USING features_metadata fd
WHERE f.features_metadata_id = fd.id AND fd.state_geo_id = :stateGeoId
""")
void deleteByStateGeoIdId(#Param("stateGeoId") UUID stateGeoId);