gpt4 book ai didi

mysql - codeigniter:类似事件记录的子句不起作用

转载 作者:行者123 更新时间:2023-11-29 21:42:34 27 4
gpt4 key购买 nike

我正在尝试将此 mysql 查询转换为事件记录,但 like 子句无法正常工作并且不返回任何输出。我实际上不明白我错过了什么。

表架构:

tbl_batch:

CREATE TABLE `tbl_batch` (
`ID` int(11) NOT NULL AUTO_INCREMENT,
`Round` varchar(5) COLLATE utf8_bin NOT NULL,
`BatchID` varchar(255) COLLATE utf8_bin NOT NULL,
`TSP` varchar(10) COLLATE utf8_bin NOT NULL,
`Slot` tinyint(1) NOT NULL,
`Trade` int(11) NOT NULL,
`StartDate` date NOT NULL,
`EndDate` date NOT NULL,
PRIMARY KEY (`BatchID`),
UNIQUE KEY `ID` (`ID`),
UNIQUE KEY `BatchID` (`BatchID`),
UNIQUE KEY `BatchID_2` (`BatchID`)
) ENGINE=InnoDB AUTO_INCREMENT=30 DEFAULT CHARSET=utf8 COLLATE=utf8_bin

tbl_tsp_信息:

CREATE TABLE `tbl_tsp_info` (
`ID` int(11) NOT NULL AUTO_INCREMENT,
`TSP` varchar(10) NOT NULL,
`Name` varchar(50) NOT NULL,
`Email` varchar(50) NOT NULL,
`Website` varchar(50) NOT NULL,
`ContactPerson` varchar(50) NOT NULL,
`ContactPhone` varchar(11) NOT NULL,
`Phone` varchar(11) NOT NULL,
`Fax` varchar(11) NOT NULL,
`Address` varchar(255) NOT NULL,
PRIMARY KEY (`ID`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4

tbl_instructor_info

CREATE TABLE `tbl_instructor_info` (
`ID` int(11) NOT NULL AUTO_INCREMENT,
`InstructorID` varchar(7) NOT NULL,
`Name` varchar(50) NOT NULL,
`Mobile` varchar(11) NOT NULL,
`Email` varchar(50) NOT NULL,
`Trade` int(1) NOT NULL,
`TSP` int(1) NOT NULL,
`Slot` tinyint(1) NOT NULL,
PRIMARY KEY (`ID`),
UNIQUE KEY `InstructorID` (`InstructorID`)
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8

MySQL 查询:(完美运行)

SELECT BatchID 
from tbl_batch
WHERE Round='7'
AND BatchID LIKE concat('%', (SELECT ShortCode FROM tbl_tsp_info WHERE id=
(SELECT TSP FROM tbl_instructor_info WHERE Email='monir.ssts@gmail.com')),'%')

Codeigniter 型号:

    public function get_batch_byUser($email=string) {
$this->db->select('TSP');
$this->db->from('tbl_instructor_info');
$this->db->where('email',$email);
$tsp = $this->db->get_compiled_select();

$this->db->select('ShortCode');
$this->db->from('tbl_tsp_info');
$this->db->where("`id`= ($tsp)", null, false);
$batchCode = $this->db->get_compiled_select();

$this->db->select('BatchID ');
$this->db->from('tbl_batch');
$this->db->where('round',7);
$this->db->like('BatchID', $batchCode);
$query = $this->db->get();

return $query->result();
}

最佳答案

我看到了问题,但解决方案可能有效,也可能无效。

以下是查看问题的方法。将代码的最后一行 return $query->result(); 替换为

echo $this->db->get_compiled_select();

你会看到

SELECT `BatchID` 
FROM `tbl_batch`
WHERE `round` = 7
AND `BatchID` LIKE '%SELECT `ShortCode`\nFROM `tbl!_tsp!_info`
\nWHERE `id`= (SELECT `TSP`\nFROM `tbl!_instructor!_info`
\nWHERE `email` = \'me@example.com\')%' ESCAPE '!'

看看通配符 (%) 在哪里?不会工作。

如果你改变

$this->db->like('BatchID', $batchCode);

$this->db->like('BatchID', "($batchCode)");

回声是这样的

SELECT `BatchID` FROM `tbl_batch` 
WHERE `round` = 7
AND `BatchID`
LIKE '%(SELECT `ShortCode`\nFROM `tbl!_tsp!_info`
\nWHERE `id`= (SELECT `TSP`\nFROM `tbl!_instructor!_info`
\nWHERE `email` = \'me@example.com\'))%' ESCAPE '!'

我不知道这是否会正确执行 - 我没有数据集。但语法看起来是正确的。对吗?

有时“Active Record”(在 Codeigniter > v3.0“查询生成器”中)并不是获得所需内容的最有效方法。回到更基本的方法通常会少很多麻烦。

$sql = "SELECT BatchID from tbl_batch WHERE Round='7' AND BatchID 
LIKE concat('%', (SELECT ShortCode FROM tbl_tsp_info
WHERE id = (SELECT TSP FROM tbl_instructor_info
WHERE Email= ?)),'%')";

$query = $this->db->query($sql, [$email]);
return $query->result();

关于mysql - codeigniter:类似事件记录的子句不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34382871/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com