0

我有两张桌子

CREATE TABLE `reg_data` (
  `date` varchar(10) NOT NULL,
  `session` varchar(10) NOT NULL,
  `time` time default NULL,
  `temp_air` float NOT NULL,
  `rel_humid` float NOT NULL,
  `soil_temp_5` float NOT NULL,
  `soil_temp_20` float NOT NULL,
  `soil_temp_30` float NOT NULL,
  `soil_temp_60` float NOT NULL,
  `air_pressure` float NOT NULL,
  `anem_reading` float NOT NULL,
  `dry_temp` float NOT NULL,
  `wet_temp` float NOT NULL,
  PRIMARY KEY  (`date`,`session`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

CREATE TABLE `reg_data3` (
  `date` varchar(10) NOT NULL default '',
  `time` time NOT NULL,
  `rainfall` float default NULL,
  `evep` float default NULL,
  `max_temp` float default NULL,
  `min_temp` float default NULL,
  `sunshine_hrs` float default NULL,
  PRIMARY KEY  (`date`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

我想使用date ='".$full."传递来加入这两个表$full=$month.'/'.$day.'/'.$year;

我的查询是:

$sql ="(select A.date,A.rainfall,A.evep, A.max_temp, A.min_temp, A.sunshine_hrs,B.run , B.velocity From reg_data3 A  ,velocity B where A.date=B.date ORDER by A.date) WHERE date ='".$full."' ;

这个查询有什么问题?

4

1 回答 1

1

尝试去掉查询中的括号,并date = '". $full . "'在第一个WHERE子句后面加上AND.

您还需要指定要用于哪个表的datedate = '". $full . "',因此将其更改为A.date = '". $full . "'

$sql ="select A.date,A.rainfall,A.evep, A.max_temp, A.min_temp, A.sunshine_hrs,B.run , B.velocity From reg_data3 A,velocity B where A.date=B.date AND A.date ='".$full."' ORDER by A.date;

我还建议格式化您的查询并使用显式连接而不是隐式连接:

$sql = "
    SELECT
        A.date,
        A.rainfall,
        A.evep,
        A.max_temp,
        A.min_temp,
        A.sunshine_hrs,
        B.run,
        B.velocity
    FROM
        reg_data3 A
        INNER JOIN velocity B
            ON A.date = B.date
    WHERE
        A.date = '". $full ."'
    ORDER BY
        A.date   /* this is pointless...they're all going to be the same date */
";
于 2013-11-13T03:18:25.977 回答