getting syntax error while using LIKE in sql query - mysql

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"

Related

MYSQL Update from a select subquery

The data in the Response column is = blahblahblahTYPE=ERRORblahblah. I then run the query below to just target the 'ERROR' string.
SELECT substr(Response, instr(Response, 'ERROR'), 5)
FROM table
WHERE id = 721451
AND Status = 'false'
AND Response LIKE '%<status>Unknown</status>%'
AND date >= curdate();
The query above returns: ERROR.
I basically want to update ERROR to SUCCESS without editing the rest of the data.
Would the query below work? Is there a better way to do this?
UPDATE table
SET Response = 'SUCCESS'
WHERE (
SELECT substr(Response, instr(Response, 'ERROR'), 5)
FROM table
WHERE id = 721451
AND Status = 'false'
AND Response LIKE '%<status>Unknown</status>%'
AND date >= curdate()
);
Thanks for your help in advance!
UPDATE table
SET Response = REPLACE(Response, 'ERROR', 'SUCCESS')
WHERE id = 721451
AND Status = 'false'
AND Response LIKE '%<status>Unknown</status>%'
AND date >= curdate();
Refer https://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_replace

query issue with datetime and querying between dates

I have this query here:
$result = "SELECT forma.*, SMS_MONTIME.IDTICKET, SMS_MONTIME.MBYLLUR,SMS_MONTIME.time_added
FROM forma
LEFT JOIN SMS_MONTIME ON forma.ID = SMS_MONTIME.IDTICKET WHERE forma.data_fillim >= '".$df."' AND forma.data_fillim <= '".$dm."' ORDER BY forma.id DESC";
I get an error like this:
onversion failed when converting datetime from character string., SQL state
Now if i print $dm and $df i get:
20130621;20130621;
The query works fine if i just put it like this:
$result = "SELECT forma.*, SMS_MONTIME.IDTICKET, SMS_MONTIME.MBYLLUR,SMS_MONTIME.time_added
FROM forma
LEFT JOIN SMS_MONTIME ON forma.ID = SMS_MONTIME.IDTICKET WHERE forma.data_fillim >= '20130621' AND forma.data_fillim <= '20130621' ORDER BY forma.id DESC";
Someone help please..
Thanks
Did you try converting $dm and $df, ToString() when doing the concatenation of the SQL string?

Converting MYSQL to Codeigniter

I am trying to convert a MYSQL query to codeigniter and going no wheres real fast. I am trying to convert this query
$conn->prepare("SELECT `id`,`song`,`artist`,`album`,`track`,`mix_name`,`date` FROM `podcasts` where mix_number = (SELECT MAX(mix_number) FROM podcasts) order by track asc");
This is in my model:
//$where = '(SELECT MAX(mix_number)from podcasts)';
$this->db->select('id,song,artist,album,track,mix_name,date, link');
//$this->db->where('mix_number', '(SELECT MAX(mix_number)from podcasts)');
$this->db->order_by('track', 'asc');
$query = $this->db->get('podcasts');
return $query->result();
My problem area is in the where statement. When I comment out the where statement I get the data. Obviously not in the manner I want it.
I am doing it this way becuase my next query(s) will be
("SELECT `id`,`song`,`artist`,`album`,`track`,`mix_name`,`date` FROM `podcasts` where mix_number = **(SELECT MAX(mix_number) FROM podcasts) - 1** order by track asc")
And on down to (SELECT MAX(mix_number) FROM podcasts) - 3
Any thoughts on the proper way of writing the where statement? Thank you for yout time.
Set the third argument of where() to false to prevent CI from altering the string you pass in to the second argument, then you can do the subquery:
return $this->db
->select('id,song,artist,album,track,mix_name,date, link')
->where('mix_number', '(SELECT MAX(mix_number) from podcasts)', false)
->order_by('track', 'asc')
->get('podcasts')
->result();
https://www.codeigniter.com/userguide2/database/active_record.html$this->db->where() accepts an optional third parameter. If you set it to FALSE, CodeIgniter will not try to protect your field or table names with backticks.
For me this produces the following query:
SELECT `id`, `song`, `artist`, `album`, `track`, `mix_name`, `date`, `link`
FROM (`podcasts`)
WHERE mix_number = (SELECT MAX(mix_number) from podcasts) ORDER BY `track` asc
If you are not too particular about using CodeIgniter's Active Record syntax, you can simply use your query as is:
$sql = "SELECT `id`,`song`,`artist`,`album`,`track`,`mix_name`,`date` FROM `podcasts` where mix_number = (SELECT MAX(mix_number) FROM podcasts) order by track asc";
$this->db->query($sql);
and then use $query->result() to get your results.

embedded mysql query inside unix_timestamp

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)

grouping by non-database field

How can I group a query result by a field that is not saved in the database.
For example I want to group the result by duration which is came from subtraction of start time and end time.
here is how i find out the duration
date1= $row_TicketRS['CloseDate'];
$date2 = $row_TicketRS['OpenDate'];
$diff = abs(strtotime($date2) - strtotime($date1));
$days = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24));
if ( $days > 0)
{
$time1 = $row_TicketRS['OpenTime'];
$time2= $row_TicketRS['CloseTime'];
$t1=($time1);
$t2=($time2);
$end=('14:30');
$start=('07:30');
$n = $end- $t1;
$n2 = $t2- $start;
$Hours2 = floor(($n+$n2)+(($days-1)*7));
echo $Hours2.' Hours';
but know i do not know how to add it to the query
here is my query
$strQuery = "SELECT count(`ticket`.TicketID) as TotOutput, department.`DeptName` FROM `ticket`, `user`, department where ticket.OwnerID = user.EmpNo and user.`DepartmentID` = department.`DepartmentID` and OpenDate between'".$DateFrom."' And '".$DateTo."'"
It'd be better to have details, but a derived table/inline view would allow you to group by a computed value:
SELECT x.duration,
COUNT(*)
FROM (SELECT t.col,
t.end_time - t.start_time AS duration
FROM YOUR_TABLE t) x
GROUP BY x.duration
How about adding that computed value to the query with an alias like this:
SELECT some_fields, end - start AS duration FROM table ORDER BY duration
dont put alias for hidden column , use directly
exmaple:
SELECT id, FLOOR(value/100)
FROM tbl_name
GROUP BY id, FLOOR(value/100);
Reference
MySQL permits expressions in GROUP BY
clauses, so the alias is unnecessary: