【发布时间】:2013-03-23 18:02:33
【问题描述】:
我正在尝试进行 SQL 查询,但遇到了一些问题。
CREATE table entries (
id_entry INT PRIMARY KEY,
);
CREATE table entry_date (
entry_date_id INT PRIMARY KEY,
entry_id INT,
entry_price INT,
entry_date TEXT,
);
每个条目都有多个日期。
我想选择 entry.entry_id,其中该条目具有链接,例如日期“23/03/2013”和“24/03/2013”。
两个日期存储到一个数组中:
$data = array('ci' => '23/03/2013', 'co' => '24/03/2013');
出于实际治疗目的,我将日期存储在文本中。 我使用 Zend_Db,所以我的查询是这样构造的:
$select = $table->select ()->from ( 'entries' )->setIntegrityCheck ( false );
if ($data ['ci'] != NULL) {
$select->join ( array (
'entry_dates' => 'entry_dates'
), 'entries.id_entry = entry_dates.entry_id' );
$select->where ( 'entry_dates.entry_date = ?', $data ['ci'] );
}
if ($data ['co']) {
if ($data['ci'] == NULL) {
$select->join ( array (
'entry_dates' => 'entry_dates'
), 'entries.id_entry = entry_dates.entry_id' );}
$select->where ( 'entry_dates.entry_date = ?', $data ['co'] );
}
给出:
SELECT `entries`.*, `entry_date`.*
FROM `entries`
INNER JOIN `entry_dates`
ON entries.id_entry = entry_dates.entry_id
WHERE (entry_dates.entry_date = '23/03/2013')
AND (entry_dates.entry_date = '24/03/2013')
而且,嗯......它不起作用。 当我获取我的 $select 时,我什么也得不到。
我想我在执行 WHERE ... AND 时错过了请求中的某些内容,我应该怎么做才能获得正确的输出?真正的请求真的很长,如果可能的话,我想避免另一个长的子选择。
【问题讨论】:
-
(entry_dates.entry_date = '23/03/2013') AND (entry_dates.entry_date = '24/03/2013'),所以您希望日期同时等于 23/03/2013 和 24/03/2013?我也很确定 MySQL 不喜欢这种格式的日期文字。 -
我知道这一点,但我如何指定我希望这 2 个日期来自 2 个不同的 entry_date ?我想获得有 2 个 entry_dates 为 '23/03/2013' 和另一个 '24/03/2013' 的条目
-
您想只选择entry.entry_id 还是同时选择entry_date 表中的数据?
标签: mysql sql zend-framework zend-db