我如何优化这个原始 SQL 并可能通过 CodeIgniter 实现它?
自从我编写原始 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
WHERE
子句(不需要第二个)JOIN
而不是WHERE
会好得多。*
) 中的所有列吗?所以我将其重构为
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%')
WHERE
clause (you don't need the second)JOIN
rather thenWHERE
.*
)?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%')
使用 a1ex07 的查询:
活动记录:
要指定字段列表,您可以执行
$this->db->select('field_1, field_2, ...');
。http://codeigniter.com/user_guide/database/active_record.html
Using a1ex07's query:
Active record:
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