1

i'm trying to develop a booking system for hotel. in my search form i've two datepicker (date departure and date arrival), and the destination. I've a problem with date, in my database i've two tables "prices" and "season" that can be joined with jointure via the id of season .

structure of tables:

CREATE TABLE IF NOT EXISTS `prices` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `prix_single` varchar(255) NOT NULL,
  `prix_double` varchar(255) NOT NULL,
  `prix_triple` varchar(255) NOT NULL,
  `idseason` varchar(255) NOT NULL,
  `idhotel` int(11) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=41 ;


CREATE TABLE IF NOT EXISTS `season` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `begin_date` varchar(255) NOT NULL,
  `end_date` varchar(255) NOT NULL,
  `idhotel` varchar(255) NOT NULL,
  PRIMARY KEY (`id`)
)

for exemple for season a i've the periode of 1 January to 28 of February (100 $ the night per person) 1 March to 28 of April (120 $ the night per person)

if my client select the period [26 February to 3 March] how can I calculate it with SQL? i've tried this:

SELECT *
FROM season, prices
WHERE season.id=prices.season
and (season.begin_season BETWEEN 2012/02/26 AND 2012/03/03 OR season.fin_season BETWEEN 2012/02/26 AND 2012/03/03)

it didn't works for me, it's return to me empty result, i've in my Database two seasons

id  begin_season    end_season  idhotel
32  10/07/2012  28/09/2012  52
35  29/09/2012  29/10/2012  52

How can i do this? Thanks a lot!

Edit: i've changed my tables and my query like this:

CREATE TABLE IF NOT EXISTS `season` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `begin_season` date NOT NULL,
  `end_season` date NOT NULL,
  `idhotel` varchar(255) NOT NULL,
  PRIMARY KEY (`id`)
) 

INSERT INTO `season` (`id`, `begin_season`, `end_season`, `idhotel`) VALUES
(32, '2012-08-09', '2012-09-09', '52'),
(35, '2012-09-10', '2012-10-30', '52');


CREATE TABLE IF NOT EXISTS `prices` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `price_single` varchar(255) NOT NULL,
  `price_double` varchar(255) NOT NULL,
  `price_triple` varchar(255) NOT NULL,
  `idseason` varchar(255) NOT NULL,
  `idhotel` int(11) NOT NULL,
  PRIMARY KEY (`id`)
)

INSERT INTO `prices` (`id`, `price_single`, `price_double`, `price_triple`, `idseason`, `idhotel`) VALUES
(31, '10', '20', '30', 32, 52),
(32, '20', '40', '60', 35, 52),

SELECT * FROM season, prices WHERE season.id=prices.idseason and (season.begin_season BETWEEN '2012-09-30' AND '2012-10-27' OR season.end_season BETWEEN '2012-09-30' AND '2012-10-27') 

My result is empty.

4

3 に答える 3

0

begin_date と end_date を varchar 以外にすることができます。おそらく、タイムスタンプまたは UNIX 時間を表す整数です。

于 2012-10-04T18:56:00.627 に答える
0

seasonテーブルが正しくセットアップされていません。

begin_dateそして…ではないend_dateか、またはvarchar(255)datedatetimetimestamp

したがって、「BETWEEN」は使用できません。それらを日付/日時/タイムスタンプに変換すると、必要なすべての機能 (>、<、BETWEEN など) を使用できます。

于 2012-10-04T18:57:12.160 に答える
0

日付のフォーマットが間違っていると思います。日付の形式は次のとおりです。

The DATE type is used for values with a date part but no time part. MySQL retrieves and displays DATE values in 'YYYY-MM-DD' format.

ここのドキュメントに従って:

http://dev.mysql.com/doc/refman/5.0/en/datetime.html

それを変更すると何か効果がありますか?

編集:

ああ、タイプとして DATE さえ使用していません。あなたはそれをすべきです。

于 2012-10-04T18:57:17.570 に答える