I have a mysql query runnning in php .
$result = mysql_query("SELECT status,initiated_by,create_timestamp,extension_id,
product_code,prospect_name,prospect_email1,prospect_phone1,prospect_title,
prospect_company,prospect_message,track_id
FROM prospect_requests
WHERE subscriber_id = '$subscriberid'
AND create_timestamp >='$start_date'
AND create_timestamp <= '$end_date'");
There is two attributes names $start_date and $end_date
My question is why above query is not returning the information of $end_date
Here is scenario .
I have selected $start_date as 01-02-2013 and $end_date as 09-2-2013 .
But the above query is not returning the details of 9 Feburary .
Please help me out .
Update
When i am placing date as 10 feb it is returning me the details of 9 feb
Table structure
CREATE TABLE `prospect_requests` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`customer_id` varchar(255) NOT NULL,
`subscriber_id` varchar(255) NOT NULL,
`extension_id` varchar(255) DEFAULT NULL,
`vendor_number` varchar(255) DEFAULT NULL,
`product_code` varchar(255) DEFAULT NULL,
`prospect_email1` varchar(255) DEFAULT NULL,
`prospect_phone1` varchar(255) DEFAULT NULL,
`prospect_email2` varchar(255) DEFAULT NULL,
`prospect_phone2` varchar(255) DEFAULT NULL,
`prospect_title` varchar(255) DEFAULT NULL,
`prospect_company` varchar(255) DEFAULT NULL,
`prospect_name` varchar(255) DEFAULT NULL,
`prospect_message` text,
`create_timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`update_timestamp` datetime DEFAULT NULL,
`status` int(11) DEFAULT '0',
`track_id` varchar(255) NOT NULL,
`initiated_by` varchar(255) DEFAULT NULL,
`subscriber_email` varchar(255) DEFAULT NULL,
`vendor_email` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=142 DEFAULT CHARSET=latin1;
Input : $start_time = 2013-02-01
$end_time = 2013-02-09
The condition <= '$end_date' is basically the same as <= '$end_date 00:00:00'.
If you need the day itself, you could query like this:
... create_timestamp <= '$end_date 23:59:59'
create_timestamp BETWEEN '$start_date' AND '$end_date 23:59:59' would work too.
Alternative
Add one day programmatically and use <:
$end_date = date('Y-m-d', strtotime("$end_date +1 day"));
... create_timestamp < '$end_date'
Update
You can also cast the timestamp to a date first:
DATE(create_timestamp) BETWEEN '$start_date' AND '$end_date'
Dates should be in the format YYYY-MM-DD.
Also, you can use WHERE create_timestamp BETWEEN '...' AND '...' as a more efficient condition for bounding a value.
for compare dates you should your BETWEEN Operator in mysql Query like Following
$result = mysql_query("SELECT
status,initiated_by,create_timestamp,extension_id,product_code,prospect_name,
prospect_email1,prospect_phone1,prospect_title,prospect_company,prospect_message,
track_id from prospect_requests WHERE subscriber_id = '$subscriberid' AND
create_timestamp BETWEEN '$start_date' AND '$end_date'");
Please full description of between operator at w2school
if still not working it then please update your question with table structure
i see your update question you are using timestamp field type that cause problem please see following link that might be helpfull Comparing timestamp with date variable (MySQL and PHP)
Related
I have a mysql query in php that is set to run hourly by a cron job. Sometimes the results are correct and sometimes I get back a result thats on a different date than being queried. As mysql is being run in a different timezone i am using php date() time() to correct the current hour and date. I first use count in mysql to every see if any records exist matching the criteria. And if they do i run the same query and grab the data. I am looking for records that are for the current day. Have a time frame of an hour or hour and a half ahead of the current time and match a couple other parameters being the JobStatus and TechID columns. Sometimes it runs correctly and sometimes I get records showing that are for days ahead. The column for the time in the database is a varchar as I just simply needed the current time to match a string as the appointment is entered into the database as 1PM or 9:30AM or whatever time slot was selected on the form in hour or half hour time frames. I have as well added my database structure below too. I have checked the database to see if its corrupt and it checks out fine.
Am I maybe going about this query incorrectly?
date_default_timezone_set("America/Chicago");
$SearchDate = date('Y-m-d', time());
//CURRENT TIME PLUS ONE HOUR
$TimePlusHour = date('gA', strtotime("+1 hours"));
//CURRENT TIME PLUS ONE HOUR PLUS SETTING MINUTES TO HALF HOUR
$Plus30 = date('g:30A', strtotime("+1 hours"));
$sqlCount = "SELECT COUNT(*) AS 'count' FROM ServiceTickets WHERE Date=
'".$SearchDate."' AND Time='".$Plus30."' OR Time='".$TimePlusHour."'
AND JobStatus='1' OR JobStatus='3' AND TechID= '".$TechID."' ";
$sql1 = "SELECT * FROM ServiceTickets WHERE Date= '".$SearchDate."' AND
Time='".$Plus30."' OR Time='".$TimePlusHour."' AND JobStatus='1' OR
JobStatus='3' AND TechID= '".$TechID."' ORDER BY id ASC";
Database structure
CREATE TABLE `ServiceTickets` (
`id` bigint(11) unsigned NOT NULL AUTO_INCREMENT,
`FirstName` varchar(255) DEFAULT NULL,
`LastName` varchar(255) DEFAULT NULL,
`Phone` varchar(20) DEFAULT NULL,
`Address` varchar(255) DEFAULT NULL,
`Address2` varchar(255) DEFAULT NULL,
`City` varchar(100) DEFAULT NULL,
`State` varchar(50) DEFAULT NULL,
`Zip` varchar(10) DEFAULT NULL,
`Email` varchar(255) DEFAULT NULL,
`Date` date DEFAULT NULL,
`Time` varchar(50) DEFAULT NULL,
`JobDesc` text,
`JobStatus` varchar(10) DEFAULT NULL,
`TechID` varchar(3) DEFAULT NULL,
`LastModified` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
)
ENGINE=MyISAM AUTO_INCREMENT=21 DEFAULT CHARSET=latin1;
You are confusing your query when calling AND and OR, I would start by putting your OR in parentheses
$sqlCount = "SELECT COUNT(*) AS 'count' FROM ServiceTickets WHERE Date=
'".$SearchDate."' AND (Time='".$Plus30."' OR Time='".$TimePlusHour."')
AND (JobStatus='1' OR JobStatus='3') AND TechID= '".$TechID."' ";
$sql1 = "SELECT * FROM ServiceTickets WHERE Date= '".$SearchDate."' AND
(Time='".$Plus30."' OR Time='".$TimePlusHour."') AND (JobStatus='1' OR
JobStatus='3') AND TechID= '".$TechID."' ORDER BY id ASC";
for e.g. if i pick date from datepicker and if that day is wednesday i want to get all data from "wed" column having particualr value only.....
i need php code and mysql qyery for this??
-- Table structure for table `list`
--
CREATE TABLE `list` (
`id` int(11) NOT NULL auto_increment,
`Mon` char(1) default NULL,
`Tue` char(1) default NULL,
`Wed` char(1) default NULL,
`Thur` char(1) default NULL,
`Fri` char(1) default NULL,
`Sat` char(1) default NULL,
`Sun` char(1) default NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=20 ;
For php code
$weekday[0]="Sun";
$weekday[1]="Mon";
$weekday[2]="Tues";
$weekday[3]="Wed";
$weekday[4]="Thur";
$weekday[5]="Fri";
$weekday[6]="Sat";
$date = '2011/10/14';
$dayName = date('D', strtotime($date));
$dayNumber = date('N', strtotime($date));
echo $dayName; //date name
$prefix = $weekday[$dayNumber]; //custom date name
$query = "select * from table_name where $prefix = 'Y'";
I have an issue with LEFT JOIN. I do not want to use eloquent relations because I want to keep my models folder clean. I have an appointments application in which I am using "labels" and "statuses". I want to be able to filter my view based on the labels and statuses. The issue with the LEFT JOIN is that when I want to click on my edit link, it uses the "id" field from my "appointments_statuses" table, instead of the "appointments" table. Below is the relevant code:
My controller:
$appointments = $query->orderBy('appointment', 'asc')
->leftJoin('appointments_labels','appointments_labels.id','=','appointments.label_id')
->leftJoin('appointments_statuses','appointments_statuses.id','=','appointments.status_id')
->get();
My view:
#foreach($appointments as $appointment)
{{ $appointment->id }} // Problem here, it uses the "status_id" field from the "appointments" table instead of the "id" field
#endforeach
My database tables:
CREATE TABLE IF NOT EXISTS `appointments` (
`id` int(11) NOT NULL,
`appointment` varchar(255) NOT NULL,
`location` varchar(255) NOT NULL,
`description` text NOT NULL,
`start` datetime NOT NULL,
`end` datetime NOT NULL,
`label_id` int(11) NOT NULL,
`status_id` int(11) NOT NULL,
`contact` int(11) NOT NULL,
`created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00'
)
CREATE TABLE IF NOT EXISTS `appointments_labels` (
`id` int(11) NOT NULL,
`label` varchar(255) NOT NULL,
`created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00'
)
CREATE TABLE IF NOT EXISTS `appointments_statuses` (
`id` int(11) NOT NULL,
`status` varchar(255) NOT NULL,
`flag` varchar(255) NOT NULL,
`created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00'
)
Well, that's because your query collects ALL the fields of the 3 tables, so the columns with the same name get overwritten.
Simply use a select() on what fields you want (which is a good practice, anyway):
$appointments = $query->orderBy('appointment', 'asc')
->leftJoin('appointments_labels','appointments_labels.id','=','appointments.label_id')
->leftJoin('appointments_statuses','appointments_statuses.id','=','appointments.status_id')
->select('appointments.id', 'appointments.name', '........', 'appointments_statuses.name', 'appointments_labels.name')
->get();
NB: I'm guessing the fields you want from the main and the joined tables, but you get the idea :)
NB2: You can also pass an array of values to the select() method:
->select(['appointments.id', 'appointments.name', ....])
I have such table
CREATE TABLE IF NOT EXISTS `superTable` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`lotID` bigint(20) NOT NULL,
`characterID` bigint(20) NOT NULL,
`confirmChoice` varchar(255) DEFAULT NULL,
`confirmStatus` varchar(255) DEFAULT NULL,
`dateCreate` datetime DEFAULT NULL,
`dateStart` datetime DEFAULT NULL,
`dateEnd` datetime DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=0 ;
I need such order
First entries where confirmChoice is null.
Next entries where confirmChoice and dateStart is not null.
All next order by dateEnd;
How can i do it in one query?
If I understood your requirement correctly , this could be one way of achieving it :
SELECT
*
,CASE
WHEN (`confirmChoice` IS NULL) THEN '1'
WHEN (`confirmChoice` IS NOT NULL AND `dateStart` IS NOT NULL ) THEN '2'
ELSE '3'
END AS sort_order
FROM
`supertable`
WHERE
1
ORDER BY
sort_order
,`dateEnd`
You probably would need to tweak it to suit your requirement .
I'm working on a codeigniter project and I'm trying to troubleshoot a sql issue. I have a query that updates a date field in my table and it's not updating it at all.
I have this table
CREATE TABLE `Customer` (
`customer_id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`first_name` varchar(55) NOT NULL DEFAULT '',
`last_name` varchar(55) NOT NULL DEFAULT '',
`city` varchar(255) DEFAULT '',
`state` char(2) DEFAULT '',
`zip` int(5) DEFAULT NULL,
`title` varchar(255) NOT NULL DEFAULT '',
`image` varchar(255) DEFAULT '',
`blurb` blob,
`end_date` date DEFAULT NULL,
`goal` int(11) DEFAULT NULL,
`paypal_acct_num` int(11) DEFAULT NULL,
`progress_bar` enum('full','half','none') DEFAULT NULL,
`page_views` int(11) NOT NULL DEFAULT '0',
`total_pages` int(11) NOT NULL DEFAULT '0',
`total_conversions` int(11) NOT NULL DEFAULT '0',
`total_given` int(11) NOT NULL DEFAULT '0',
`conversion_percentage` int(11) NOT NULL DEFAULT '0',
`avg_contribution` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`customer_id`)
) ENGINE=MyISAM AUTO_INCREMENT=15 DEFAULT CHARSET=utf8;
and when I run this query to insert data, it runs fine and sets the date to 2012-11-01
INSERT INTO `Customer` (`first_name`, `last_name`, `end_date`) VALUES ('John', 'Smith2', '2012-11-01');
Then I get the customer_id and try to run this query
UPDATE `Customer` SET `end_date` = '2012-14-01' WHERE `customer_id` = '18';
and it sets the date end_date field to 0000-00-00.
Why is it changing the end date to 0000-00-00 rather than 2012-14-01?
2012-14-01 is first day of fourteenth month :)
(so its invalid date, thus casted to 0000-00-00 and Data truncated for column 'end_date' at row 1 warning was returned by mysql, which you can see by querying SHOW WARNINGS to mysql immediately after badly behaving query)
2012-01-14 is 14th of January.
use this:
UPDATE `Customer` SET `end_date` = date('Y-m-d') WHERE `customer_id` = '18';
Use date function to update this field.