Why is this not working
...
) as Data
WHERE UNIX_TIMESTAMP(Data.DateTime) <= UNIX_TIMESTAMP(SELECT DateTime from mytable WHERE ID = $inputID)
It seems to work if I don't have this embedded sql statement, but the sql statement works on its own also so I'm not sure why the combination is causing a failure.
It's telling me
check to your MySQL server version for the right syntax to use near
'SELECT DateTime from mytable WHERE ID = 1008)' at line 1
Try surrounding the SQL with additional parens ():
WHERE UNIX_TIMESTAMP(Data.DateTime) <= UNIX_TIMESTAMP((SELECT DateTime from mytable WHERE ID = $inputID))
Use this:
...
) as Data
WHERE UNIX_TIMESTAMP(Data.DateTime) <= (SELECT UNIX_TIMESTAMP(DateTime) from mytable WHERE ID = $inputID)
If DateTime has type DateTime than you can also use:
...
) as Data
WHERE Data.DateTime <= (SELECT DateTime from mytable WHERE ID = $inputID)
Related
update fare_strategy set sales_date_to = ’07-SEP-22’
where dep_date_to is not null
and market_id in
(select market_id
from fare_strategy_markets
where set_type = ‘STANDARD’
);
In the following query, I'm getting error as SQL command not properly ended.
try to change like this because in your query you used ’ this is not valid so i just change it to ' so it's works fine
update fare_strategy set sales_date_to = '07-SEP-22' where dep_date_to is
not null and market_id in (select market_id from
fare_strategy_markets where set_type = 'STANDARD');
When I create sql query like this:
param = (date, )
query = SELECT date_added from deploy_requests WHERE date_added LIKE '%s %'
query.execute(query, param)
i get Syntax error
but if I use the not recommended way
query = SELECT date_added from deploy_requests WHERE date_added LIKE '{} %'.format(date)
I figured out, this is how I did it.
def get_deployment_times(date):
date = "{}%".format(date)
param = (date, )
query = "SELECT date_added from deploy_requests WHERE date_added LIKE %s"
Hi I'm trying to write a cursor in powerbuilder 12 to fetch some records . Here this is my query.
I'm trying to fetch some records from the trninvhdr table which are not in the second table.
SELECT
INV_DATE,
INV_NO,
INV_TYPE,
CUR_CODE,
EXCH_RATE,
usd_rate,
CR_TERM,
DUE_DATE,
bl_date,
TOT_AMT
FROM trninvhdr
WHERE
COMP_CODE ='NFL1' AND
CUST_CODE = 'NLML' AND
INV_TYPE ='F' AND
INV_DATE <= '2016-03-25' AND
NOT EXISTS
(SELECT * FROM trninvoiceavailability WHERE trninvoiceavailability.COMP_CODE = trninvhdr.COMP_CODE
AND trninvoiceavailability.INV_TYPE = trninvhdr.INV_TYPE AND trninvoiceavailability.INV_NO = trninvhdr.INV_NO);
Here how I use it in the program.
DECLARE lc_retrieve CURSOR FOR
SELECT
trninvhdr.INV_DATE,
trninvhdr.INV_NO,
trninvhdr.INV_TYPE,
trninvhdr.CUR_CODE,
trninvhdr.EXCH_RATE,
trninvhdr.usd_rate,
trninvhdr.CR_TERM,
trninvhdr.DUE_DATE,
trninvhdr.bl_date,
trninvhdr.TOT_AMT
FROM trninvhdr
WHERE
COMP_CODE = :as_comp_code AND
CUST_CODE = :as_cust_code AND
INV_TYPE ='F' AND
INV_DATE <= :as_inv_date )AND
NOT EXISTS (SELECT * FROM trninvoiceavailability
WHERE trninvoiceavailability.COMP_CODE = trninvhdr.COMP_CODE
AND trninvoiceavailability.INV_TYPE = trninvhdr.INV_TYPE AND
trninvoiceavailability.INV_NO = trninvhdr.INV_NO);
open lc_retrieve ;
The query works fine in the mysql server but in the progra it gives me the following error .
Database c0038 SQLSTATE = 3700 MySQL ODBC 5.2 a Driver mysql id 5.5.25 You have an error in your syntax. check the manual that corresponds to your mysql version for the right syntax to use near NOT EXISTS (SELECT * FROM trninvoiceavailability.... at line 1.
What is the correct Syntax that I should use to work this query.
I can see a bracket in this code... where did it come from and where is it's friend?
INV_DATE <= :as_inv_date )AND
You need to remove ) from your query as per below-
INV_DATE <= :as_inv_date )AND
sholuld be INV_DATE <= :as_inv_date AND
I try to change my code from MYSQL to SQL and i got an error (SQL Syntax 'Limit').
So i tried to change my query and update with "TOP" but seems to work only with SELECT.
So, how can i change this MYSQL query :
$fct="UPDATE `users` SET `STREAM_TITRE` = '$STREAM_TITRE',`STREAM_URL` = '$STREAM_URL',`STREAM_DESC` = '$STREAM_DESC',`STREAM_GENRE` = '$STREAM_GENRE' WHERE `ID` =$IDSESS LIMIT 1";
Here is my SQL Code without Limit :
$fct="UPDATE users SET STREAM_TITRE = '$STREAM_TITRE', STREAM_URL = '$STREAM_URL', STREAM_DESC = '$STREAM_DESC', STREAM_GENRE = '$STREAM_GENRE' WHERE ID = '$IDSESS'";
Thanks
It's not very clear which version of your query is working and which is not - and in what DBMS.
If ID is of char or varchar type, you are missing some quotes in the LIMIT version. Although MySQL is not very picky and you won't have many issues, with or without quotes:
$fct = "
UPDATE users
SET STREAM_TITRE = '$STREAM_TITRE'
, STREAM_URL = '$STREAM_URL'
, STREAM_DESC = '$STREAM_DESC'
, STREAM_GENRE = '$STREAM_GENRE'
WHERE ID = $IDSESS --<-- this should be '$IDSESS' , right?
----- or $IDSESS , depending on the datatype
LIMIT 1
";
Note: The LIMIT n works in MySQL and PostgreSQL, but not in some other DBMS. Plus, I don't think you really need it anyway, as the ID is probably the Primary Key of the table.
If you are trying to convert the statement from MySQL to SQL-Server, you should not use the backquotes and replace LIMIT 1 with TOP (1):
$fct = "
UPDATE TOP (1) users
SET STREAM_TITRE = '$STREAM_TITRE'
, STREAM_URL = '$STREAM_URL'
, STREAM_DESC = '$STREAM_DESC'
, STREAM_GENRE = '$STREAM_GENRE'
WHERE ID = $IDSESS
";
I need to find all rows in a MySQL database that fall between "YYYY-10-30" and "YYYY-12-11" I don't care about which year the the date is in just that falls between the two dates.
SELECT *
FROM yourTable
WHERE (MONTH(yourDate) = 10 AND DAYOFMONTH(yourDate) >= 30)
OR (MONTH(yourDate) = 11)
OR (MONTH(yourDate) = 12 AND DAYOFMONTH(yourDate) <= 11)
select *
from table
where
concat(year(now()),right(date_field,6))
between
concat(year(now()),'-10-30')
and
concat(year(now()),'-12-11')
Could you try this?:
CREATE DEFINER=`root`#`localhost` PROCEDURE `GetByMonthAndDayRange`
(
IN monthFrom INT
, IN dayFrom INT
, IN monthTo INT
, IN dayTo INT
)
BEGIN
-- For good coding practice, name your columns instead of using *
SELECT *
FROM tablename
WHERE
(MONTH(datecolumnname) = monthFrom AND DAYOFMONTH(datecolumnname) >= dayFrom)
OR (MONTH(datecolumnname) > monthFrom AND MONTH(datecolumnname) < monthTo)
OR (MONTH(datecolumnname) = monthTo AND DAYOFMONTH(datecolumnname) <= dayTo);
END
If you use a stored procedure (and if the code works to your taste) you'll be able to code faster, just providing the parameters (it's dynamic this way)