db->distinct(); $t-6ren">
gpt4 book ai didi

php - codeigniter 中的 union 我的 sql 错误

转载 作者:行者123 更新时间:2023-11-29 12:25:38 24 4
gpt4 key购买 nike

实际上我想要使用多字段进行搜索,所以我决定联合查询并编写以下代码

$this->db->select("id, title, job_type");
$this->db->distinct();
$this->db->from("jobs");
$this->db->like('title',$keyword);
$this->db->get();
$query1 = $this->db->last_query();

$this->db->select("company");
$this->db->distinct();
$this->db->from("employers");
$this->db->join('jobs', 'jobs.userId = employers.userId');
$this->db->or_like('company',$company_name);

$this->db->get();
$query2 = $this->db->last_query();
$query = $this->db->query($query1." UNION ".$query2);

return $query->result();

如上面的代码生成以下 sql 查询语句,如下所示

SELECT DISTINCT `id`, `title`, `job_type` FROM (`jobs`) WHERE `title` 
LIKE '%dfsdf%' UNION SELECT DISTINCT `company` FROM (`employers`) JOIN
`jobs` ON `jobs`.`userId` = `employers`.`userId` WHERE `company` LIKE '%%'

它显示以下错误

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'JOIN jobs ON jobs.userId = employers.userId WHERE company LIKE '%%' at line 5

谁能帮帮我

最佳答案

首先,为了使用union,没有。两个查询中的列数及其数据类型应匹配。

您正在尝试在一个选择三列的查询与另一个仅选择一列的查询之间进行并集。这是行不通的。

要了解更多关于mysql unions的信息,您可以通过以下链接 http://dev.mysql.com/doc/refman/5.0/en/union.html

关于php - codeigniter 中的 union 我的 sql 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28363606/

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