Retrieve row data according to the column value - php

I am trying to get the renewal_date column and get the month to check is that the exact month I want, and if so display the details of that record. My query is:
select domain_name, renewal_date
from hosting_details
where substr(renewal_date,-5,-3) = '09'
This doesn't give any result, but when I run below query I can retrieve results:
select domain_name, renewal_date
from hosting_details
where renewal_date = '2015-09-03'
I don't want to hard code the date, I want to retrieve any details which is in 09th month. How can I do it?

You should not perform a function on your (primary) WHERE statement. It will not use the index and perform a full table scan.
SELECT domain_name,renewal_date FROM hosting_details
WHERE renewal_date >= '01-09-2015' AND renewal_date < '01-10-2015'

You can try as below :
select domain_name,renewal_date from hosting_details where
date_format(renewal_date, '%m' ) = '09'

Related

how to select alternate rows from mysql using php

I want to select rows alternatively in mysql using php,
<?php
$result=mysql_query("select * from marker1 where date='$maxdate' and time BETWEEN '$mintime' AND '$maxtime' and imei_no='$vehicle_imei_no1'")or die(mysql_error());
?>
this is my query but it select all rows, in database i m not getting serialy data so i can't use this query
select t.id, name
from (select id, name, ROW_NUMBER() over (order by id) as srNo from Employee) t
where (t.srNo % 2) = 1
i only differentiate by time '11:05:30' in this formate
please guide me how to use query for alternative rows.
Thanks
Maybe this would work!:
select * from marker1 where date='$maxdate' and time BETWEEN '$mintime' AND '$maxtime' and imei_no='$vehicle_imei_no1'
AND (marker1.ID % 2) == 0
marker1.ID is the primary key and I'm supposing it as auto_increment.
This should select only the even values.
$sql = "SELECT * FROM Orders LIMIT 15, 10";
It would start from record 15th and return 10 records.
Ref: Link

Mysql query to select records where the sum of a column's values is in a given variable range

I've been trying to write a query to select all records from a table where the sum of a column's values is between a variable range.
Here's what I came up with so far:
$result = mysql_query(' SELECT * FROM table WHERE SUM(column) BETWEEN $Range1 AND $Range2 ORDER BY RAND());
However when I try to loop through the above query with the mysql_fetch_object function it gives me a common error (The supplied argument is not a valid result).I've tried different ways of writing it but still come up short
So I tried the query using aliases you guys provided but still get the same error.
$result = mysql_query(' SELECT column1, SUM(column2) AS Total FROM table GROUP BY column1 HAVING Total BETWEEN $Range1 AND $Range11 ORDER BY RAND()');
I'm not sure what the "ordering by rand" is needed for but your final query will look something like this:
SELECT *, SUM(column) AS `total`
FROM table
GROUP BY someColumn
HAVING `total` BETWEEN $Range1 AND $Range2
ORDER BY RAND()
;

How to use current date in mysql query?

I have a database that contains a column with type - Date. I also have a query with the date inputted as static which works fine but I would like to use todays date in the query. any recommendations?
Query :
$q = 'SELECT count(ID) as count FROM ORDER WHERE
ASSIGN_TO ='.$db->qstr($person).' AND OPEN_DATE ='.$db->qstr('2014-05-14');
This currently displays count of items after 2014-05-14
You could use the NOW() function that returns the current date. To avoid skewed answered by hours/minutes/seconds, you can use date to extract the date part:
$q = 'SELECT count(ID) as count FROM ORDER WHERE
ASSIGN_TO ='.$db->qstr($person).' AND DATE(OPEN_DATE) = DATE(NOW())';

fetching record according current date

can any one explain how i would display my table record according date, like any user of my website can see their activity of current date only...
while($data=mysql_fetch_array($rs))
{
$data["create_date"]=date("M d,y" , $data[create_date]);
i am using this but it displaying previous date result also
here is my code, i am fetching result from different tables
$SQL="select * from $tab where 1 $con ORDER BY id desc $_REQUEST[sort_mode] $con_limit";
while($data=mysql_fetch_array($rs))
{
$data["create_date"]=date("M d,y" , $data[create_date]);
gri("users","WHERE id='$data[op_id]' ","",$op_name);
gri("patient", " where id ='$data[patient_id]' order by id desc","",$patient);
$data[first_name]=$patient[first_name];
$data[last_name]=$patient[last_name];
$data[age]=$patient[age];$data[sex]=$patient[sex];
$data[mob]=$patient[mob];
$data[op_name]=$op_name[name];
$t->set_var($data);
$t->set_var(array("chk_status_$data[id]_$data[status]"=>"selected",));
$t->parse("ABlockList","AccessBlockList",true);
}
You can also use MySQL for this.
Example
select * from table_name where create_date >= NOW();
OR
SELECT * FROM table_name WHERE DATE(create_date) = CURDATE();
OR
SELECT * FROM table_name WHERE DATE(create_date) = DATE(NOW())
DATE() returns the date without time and NOW() returns the current date & time (note we’ve used the DATE() function in this query to remove the time)

How to select certain data from a same field

Now currently facing a problem that is..
My table name is schedule
One of the data field is 'Depart'
the record is
row 1 = 19-08-2012 08:00:00AM,
row 2 = 19-08-2012 12:00:00PM,
row 3 = 20-08-2012 07:00:00PM,
I just want to display the date only and it is distinct
mysql_query(Select distinct depart from schedule);
This display date and the time.
Any one here know how to display the date only?
Example
SQL (recommended)
SELECT DISTINCT DATE( depart )
FROM `tbl`
PHP
$d = "19-08-2012 08:00:00AM";
echo date("d-m-Y",strtotime($d));
You can use DATE_FORMAT() function in the Mysql, as follows :
select DATE_FORMAT(distinct(Depart),'%m-%d-%Y') from schedule
Yes, use DATE:
SELECT DISTINCT DATE(`depart`) FROM `schedule`

Categories