我如何优化这个原始 SQL 并可能通过 CodeIgniter 实现它?

发布于 2024-12-05 04:08:12 字数 453 浏览 1 评论 0原文

自从我编写原始 SQL 以来已经有一段时间了,我希望有人可以帮助我优化这个 SQL 查询,以便它可以跨 MySQL 和 PostgreSQL 工作。

我还必须使用 ActiveRecord 通过 CodeIgniter (2.x) 来实现这一点,有什么帮助/建议吗?

       SELECT *
     FROM notaries, contact_notaries
    WHERE notaries.id = contact_notaries.notary_id
AND WHERE (   contact_notaries.city LIKE %$criteria%
           OR contact_notaries.state LIKE %$criteria
           OR contact_notaries.address LIKE %$criteria%)

谢谢!

It's been a while since I've written raw SQL, I was hoping someone could help me out in optimizing this SQL query so that it works across, both, MySQL and PostgreSQL.

I would also have to implement this via CodeIgniter (2.x) using ActiveRecord, any help/advice?

       SELECT *
     FROM notaries, contact_notaries
    WHERE notaries.id = contact_notaries.notary_id
AND WHERE (   contact_notaries.city LIKE %$criteria%
           OR contact_notaries.state LIKE %$criteria
           OR contact_notaries.address LIKE %$criteria%)

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

站稳脚跟 2024-12-12 04:08:12
  1. 每个查询只能有一个 WHERE 子句(不需要第二个)
  2. 将连接条件放入 JOIN 而不是 WHERE 会好得多。
  3. 您确定确实需要 2 个表 (*) 中的所有列吗?
    所以我将其重构为

SELECT [field_list]
来自公证人
内连接 contact_notaries ON (notaries.id = contact_notaries.notary_id)
WHERE ( contact_notaries.city LIKE '%$criteria%'
或者 contact_notaries.state LIKE '%$criteria'
或者 contact_notaries.address LIKE '%$criteria%')

  1. Each query can have just one WHERE clause (you don't need the second)
  2. It's much better to put join condition into JOIN rather then WHERE.
  3. Are you sure you really need all the columns from 2 tables (*)?
    So I'd refactor it to

SELECT [field_list]
FROM notaries
INNER JOIN contact_notaries ON (notaries.id = contact_notaries.notary_id)
WHERE ( contact_notaries.city LIKE '%$criteria%'
OR contact_notaries.state LIKE '%$criteria'
OR contact_notaries.address LIKE '%$criteria%')

倦话 2024-12-12 04:08:12

使用 a1ex07 的查询:

SELECT [field_list]
FROM notaries
INNER JOIN contact_notaries ON (notaries.id = contact_notaries.notary_id)
WHERE ( contact_notaries.city LIKE '%$criteria%'
OR contact_notaries.state LIKE '%$criteria'
OR contact_notaries.address LIKE '%$criteria%')

活动记录:

$this->db->select(); // Leave empty to select all fields
$this->db->join('contact_notaries', 'notaries.id = contact_notaries.notary_id', 'inner');
$this->db->like('contact_notaries.city', 'criteria');
$this->db->like('contact_notaries.state', 'criteria');
$this->db->like('contact_notaries.address', 'match');
$results = $this->db->get('notaries');

要指定字段列表,您可以执行 $this->db->select('field_1, field_2, ...');

http://codeigniter.com/user_guide/database/active_record.html

Using a1ex07's query:

SELECT [field_list]
FROM notaries
INNER JOIN contact_notaries ON (notaries.id = contact_notaries.notary_id)
WHERE ( contact_notaries.city LIKE '%$criteria%'
OR contact_notaries.state LIKE '%$criteria'
OR contact_notaries.address LIKE '%$criteria%')

Active record:

$this->db->select(); // Leave empty to select all fields
$this->db->join('contact_notaries', 'notaries.id = contact_notaries.notary_id', 'inner');
$this->db->like('contact_notaries.city', 'criteria');
$this->db->like('contact_notaries.state', 'criteria');
$this->db->like('contact_notaries.address', 'match');
$results = $this->db->get('notaries');

To specify a list of fields you can do $this->db->select('field_1, field_2, ...');.

http://codeigniter.com/user_guide/database/active_record.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文