SQL help

Hi,

Sorry for all the posts…

I am trying to get something that checks to see if any items are available between two dates.

This is the SQL I have just now:

SELECT job_items.quantity FROM job_items LEFT JOIN jobs ON job_items.job_id = jobs.job_id WHERE product_id = ‘1000’ AND jobs.fromdate >= ‘$todate’ AND jobs.todate <= ‘$fromdate’

This then checks if the quantity out is greater than or equal to the number of products in stock. The problem is on testing it returns 0 results even when you know that there is a overlap on the date. (tested directly in phpmyadmin)

Any help?

What are your column types for fromdate and todate and the values of $todate and $fromdate

‘fromdate’ and ‘todate’ are INT. The format is the Unix Epoch.

The variables are the same format and come from a form.

Without seeing your tables and knowing if your JOIN is proper it’s hard to say what the problem could be. Do you need a left join here? Are there jobs with no job_items?

When you add a job it will always have items in that table.

Here are the two tables:

[code]CREATE TABLE jobs (
id int(5) NOT NULL AUTO_INCREMENT,
customer_id int(11) NOT NULL,
job_id int(30) NOT NULL,
contact_name varchar(40) NOT NULL,
address1 varchar(50) NOT NULL,
address2 varchar(50) NOT NULL,
address3 varchar(50) NOT NULL,
postcd varchar(8) NOT NULL,
cid varchar(50) NOT NULL,
deliver varchar(50) NOT NULL,
fromdate int(10) NOT NULL,
todate int(10) NOT NULL,
before_vat decimal(10,2) NOT NULL,
vat decimal(10,2) NOT NULL,
full_total decimal(10,2) NOT NULL,
delprice decimal(10,2) NOT NULL,
notes varchar(1000) NOT NULL,
quote varchar(10) NOT NULL,
KEY jobid (id)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

CREATE TABLE job_items (
id int(10) NOT NULL AUTO_INCREMENT,
job_id int(10) NOT NULL DEFAULT ‘0’,
quantity int(25) NOT NULL DEFAULT ‘0’,
product_id int(10) DEFAULT ‘0’,
unit_price decimal(25,2) DEFAULT ‘0.00’,
description text COLLATE utf8_unicode_ci,
total decimal(25,2) DEFAULT ‘0.00’,
before_vat decimal(10,2) NOT NULL,
vat decimal(10,2) NOT NULL,
subtotal decimal(10,2) NOT NULL,
discount varchar(20) COLLATE utf8_unicode_ci NOT NULL,
PRIMARY KEY (id)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
[/code]

Do you have these variables backwards?

jobs.fromdate >= ‘$todate’ AND jobs.todate <= ‘$fromdate’

I got the syntax from here:

http://board.phpbuilder.com/showthread.php?10219920-Checking-for-Overlapping-Dates-in-2-Date-Ranges

I’m just saying, your variables don’t match your field names so I wonder if you have them backwards. Without having the values of $fromdate and $todate I have no idea.

Its down to how to check if the dates over lap. That link explains how it works but just makes no sense to me.

Anyone help?

I have this statement too: SELECT job_items.quantity FROM job_items LEFT JOIN jobs ON job_items.job_id = jobs.job_id WHERE product_id = ‘1000’ AND ((jobs.fromdate > ‘1352251962’) AND (jobs.todate < ‘1351733562’)) Nothing. The large numbers are dates and will be made from variables posted from a form.

Your values certainly look backwards to me. Are you sure they are correct to the table?

Does this give you the results you expect?

SELECT job_items.quantity FROM job_items LEFT JOIN jobs ON job_items.job_id = jobs.job_id WHERE product_id = '1000' AND jobs.fromdate >= 1351733562 AND jobs.todate <= 1352251962

This is what Im trying to get to: http://www.flipflops.org/2008/05/01/finding-overlapping-dates-and-times-in-mysql/ but a touch more complex thats all.

Sponsor our Newsletter | Privacy Policy | Terms of Service