query issue with datetime and querying between dates - mysql

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?

Related

Convert Sql query to sql CodeIgniter

convert sql query to sql codeigniter
i done try to use this method
How to convert sql query to codeigniter active records
but not working for me...
so i try to post in here
this my sql query
$sql = "SELECT b.id, b.us_id, b.kredit, b.info,u.us_name,
u.us_username, u.us_email, u.us_phone, b.cnt, b.amnt
FROM users u JOIN
(SELECT id, us_id, kredit, info, COUNT(info) cnt, SUM(kredit) amnt
FROM balance_history
GROUP BY info HAVING cnt > 1
) AS b
ON u.us_id = b.us_id
WHERE b.kredit != '0' AND
b.info NOT LIKE '[PERBAIKAN]%' AND
(b.info LIKE 'Transfer saldo%' OR b.info LIKE 'Ket%')
ORDER BY b.id ASC";
thanks before...
If you aren't loaded database globally, you can load it by calling $this->load->database(); The following query may provide the same db call.
$this->db->select('id, us_id, kredit, info, COUNT(info) cnt, SUM(kredit) amnt')
->from('balance_history')
->group_by('info')
->having('cnt > 1');
$subquery = $this->db->get_compiled_select();
$query = $this->db
->select('b.id, b.us_id, b.kredit, b.info,u.us_name,u.us_username, u.us_email, u.us_phone, b.cnt, b.amnt')
->from('users u')
->join('('.$subquery.') b','u.us_id = b.us_id')
->where('b.kredit !=','0', true)
->not_like('b.info', '[PERBAIKAN]', 'after', false, true)
->where('(b.info LIKE "Transfer saldo%" OR b.info LIKE "Ket%")')
->get();

getting syntax error while using LIKE in sql query

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"

Convert SQL query to yii2?

Until now I have been using a SQL query in yii2 which was working fine locally but as soon as I deploy it to the server it shows
I have tried changing it to yii query since its a proper implementation below
$query = $connection->createCommand("SELECT a.name_of_flight, a.time_of_flight, (a.no_of_passenger - b.cnt) as avail, a.no_of_passenger FROM flight_schedule a LEFT JOIN (SELECT flight_time, COUNT(id) AS cnt FROM book_eticket WHERE flight_date='$date' AND company_name = '$comp_name' GROUP BY flight_time) b ON a.id = b.flight_time")->queryAll();
to
$query = (new \yii\db\Query());
$query
->select('a.name_of_flight, a.time_of_flight, (a.no_of_passenger - b.cnt) as avail, a.no_of_passenger')
->from('flight_schedule a')
->leftJoin('flight_time', ('COUNT(id) AS cnt FROM book_eticket'))
->where(array('and', 'flight_date=2016-6-29', 'company_name = Team5'))
->groupBy(['flight_time b','ON a.id = b.flight_time']);
$command = $query->createCommand();
$query = $command->queryAll();
but I get an error:
Can anybody help me find out the problem? Thanks in advance
First screen says about access issues. Second - that operands like date and team is not string. That is wrong. Can you quote them?

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.

MYSQL returning no records from URL left string

SELECT
domain_usage.url,
LEFT(url,locate('?',url)) AS cleanURL
FROM
domain_usage
WHERE
MONTH(domain_usage.login_date) = (Now()) AND
YEAR(domain_usage.login_date) = (Now());
Returns no records, and no error ?
im trying to clean the query string from URL field.....
Any thoughts ?
Your date comparisons are wrong. you're basically trying
3 = '2013-01-18 10:36:00'
you need to compare apples to apples:
MONTH(domain_usage.login_date) = MONTH(now())
modify your WHERE condtion,
SELECT domain_usage.url, LEFT(url,locate('?',url)) AS cleanURL
FROM domain_usage
WHERE MONTH(domain_usage.login_date) = MONTH(CURDATE()) AND
YEAR(domain_usage.login_date) = YEAR(CURDATE())