从数据库中获取数据时出错

时间:2013-11-13 03:07:38

标签: mysql

我有两张桌子

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."' ;

此查询有什么问题?

1 个答案:

答案 0 :(得分:1)

尝试删除查询中的括号,并将date = '". $full . "'放在第一个WHERE子句后加AND

您还需要指定要用于date的哪个表的date = '". $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 */
";