SELECT error querying - mysql

What could be wrong with this line:
'$query = "SELECT * FROM messages WHERE (rlat => '".$latmin."' AND rlat <= '".$latmax."') AND (rlon >= '".$lonmin."' AND rlon <= '".$lonmax."')";'
Error: Error in query: SELECT * FROM messages WHERE (r_lat =>
'55.4655951769' AND r_lat <= '55.496987423') AND (r_lon >=
'25.5338700398' AND r_lon <= '25.5989507602'). 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 '=> '55.4655951769'
AND r_lat <= '55.496987423') AND (r_lon >= '25.5338700398' AN' at line
1...
Thanks!

=> isn't a known operator. If you want the greater than or equal comparison operator, then you are after >=.
Incidentally, you might find that your query can be written more concisely with the BETWEEN ... AND ... operator:
SELECT *
FROM messages
WHERE rlat BETWEEN $latmin AND $latmax
AND rlon BETWEEN $lonmin AND $lonmax
You should also investigate passing variables into your SQL by way of parameters to prepared statements.

Related

Trying to perform divison using MySQL subqueries but getting syntax error

I am trying to get the value from MySQL subqueries but I am getting '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 ')' at line 1'
The code is:
UPDATE national_avg_tariff SET Value= (CASE WHEN ((SELECT * FROM((SELECT Value
/ ((Select Sum_CPP_MlnRs from ppp_fy2020_2030_annex4.FY_CPP_exclKE_MlnRs where FiscalYear=2020) + (Select UoSC_MoF_exclKE_MlnRs from ppp_fy2020_2030_annex4.FY_UoSC_MoF_exclKE_MlnRs where FiscalYear=2020))/ (SELECT Sum(Sum_of_AvgMW) FROM ppp_fy2020_2030_annex4.AVG_OF_FY_MAX_DEMAND where FiscalYear=2020 GROUP BY FiscalYear)FROM tariff_true_cost.national_avg_tariff WHERE Sector='Residential' AND Category='Up to 50 Units' AND Parameter= 'Fixed Charge(Rs/kW/M)' AND FiscalYear=2020)temptbl8))>0)
THEN
(SELECT * FROM((SELECT Value
/ ((Select Sum_CPP_MlnRs from ppp_fy2020_2030_annex4.FY_CPP_exclKE_MlnRs where FiscalYear=2020) + (Select UoSC_MoF_exclKE_MlnRs from ppp_fy2020_2030_annex4.FY_UoSC_MoF_exclKE_MlnRs where FiscalYear=2020))/ (SELECT Sum(Sum_of_AvgMW) FROM ppp_fy2020_2030_annex4.AVG_OF_FY_MAX_DEMAND where FiscalYear=2020 GROUP BY FiscalYear)FROM tariff_true_cost.national_avg_tariff WHERE Sector='Residential' AND Category='Up to 50 Units' AND Parameter= 'Fixed Charge(Rs/kW/M)' AND FiscalYear=2020)temptbl8))
ELSE NULL END) where FiscalYear=2020 AND Parameter='Fixed Charge(%)';
The code doesn't show any red underline or pre-execution error something, the error only appears once I execute it.
Any help in rectifying syntax error of this piece of code would be appreciable. Thanks

MYSQL syntax error, with date time

I am currently converting my api from SQL to MySQL, however one of my queries is failing at this point below.
The query:
SELECT u.DATETIME,
SUM(Value * m.Multiplier) * 1 AS Usage,
TIMESTAMPDIFF(SECOND, '1970-01-01 00:00:00', UNIX_TIMESTAMP(u.DATETIME)) as Datestamp_epoch
FROM MeterMaps m
JOIN UsageElectricityDaily u ON m.ChanID = u.ChanID
JOIN SourceChannels c ON c.ChanID = m.ChanID
WHERE m.LocationID = 1
AND c.QuantityID = 'Usage'
AND u.DateStamp >= '01/12/2016 00:00:00'
AND u.DateStamp < '31/12/2016 00:00:00'
AND m.Utility = 'Electric'
GROUP BY u.Datestamp
The error that is thrown is
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 'Usage, TIMESTAMPDIFF(SECOND, '1970-01-01 00:00:00',
UNIX_TIMESTAMP(u.DATETIME)) ' at line 1
In MySQL the word usage is a keyword, therefore it must be enclosed by backticks or change it.
SELECT u.DATETIME,
SUM(Value * m.Multiplier) * 1 AS `Usage`
...

CURDATE gives error

I have an INSERT statement,
$stmt = $mysqli->prepare("INSERT INTO numbertable (numbers, numdate, salesperson, color) VALUES (?, NOW(), ?, ?)");
My SELECT statement looks like this:
$result = $mysqli->query("SELECT COUNT FROM numbertable WHERE DATE(numdate) == CURDATE()");
When I run my select statement, I get the following error. Does anybody know what that could be?
SELECT COUNT FROM numbertable WHERE DATE(numdate) == CURDATE()
LIMIT 0, 30
MySQL said: Documentation
#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 '== CURDATE()
LIMIT 0, 30' at line 1
in mySQL you need = as equal sign, not ==. should be:
SELECT COUNT FROM numbertable WHERE DATE(numdate) = CURDATE()

SQL where to query in Rails

When I type:
User.where(:created_at => "2015-08-06".."2015-08-07").where("updated_at > created_at + 1.day").count
Error Returned:
SELECT COUNT(*) FROM `users` WHERE `users`.`created_at` BETWEEN '2015-08-06' AND '2015-08-07' AND (updated_at > created_at + 1.day)ActiveRecord::StatementInvalid: Mysql2::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 'day)' at line 1: SELECT COUNT(*) FROM `users` WHERE `users`.`created_at` BETWEEN '2015-08-06' AND '2015-08-07' AND (updated_at > created_at + 1.day)
from /home/guinness/shared/bundle/ruby/2.1.0/gems/activerecord-3.2.13/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:245:in query'
What's wrong with this? As you know I want to know how many users retained after 1 day of their sign up.
1.day is ruby syntax, not sql. try this:
User.where(:created_at => "2015-08-06".."2015-08-07").where("updated_at > DATE_ADD(created_at, INTERVAL 1 DAY)").count
see http://www.w3schools.com/sql/func_date_add.asp
Try it like this
User.where(:created_at => "2015-08-06".."2015-08-07").where("updated_at > ?", created_at + 1.day).count

Why is this a SQL syntax error?

I have this query in Perl:
my $pkg="%";
my $sql = "SELECT pid, CAST(pid as UNSIGNED) AS l FROM xmld ORDER BY l WHERE pkg LIKE ?";
my $files_ref = $dbh->selectcol_arrayref($sql, undef, $pkg);
It crashes with:
DBD::mysql::db selectcol_arrayref failed: 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 'WHERE pkg LIKE '%'' at line 1 at ...
I have been looking at this statement for hours, tried various things, but no luck.
Where is that extra single quote coming from and how do I get rid of it?
order by goes after the where:
SELECT pid, CAST(pid as UNSIGNED) AS l
FROM xmld
WHERE pkg LIKE ?
ORDER BY l;