admin管理员组文章数量:1130349
I have 2 tables I need to search.
1st table Name: Personnel Fields: id, lname, fname, department, extension
2nd table Name: department Fields: id, department
The 2nd table has the full department name, while the 1st table has the id associated with the department. When the user searches, it should search for the lname, fname, and extension from the 1st table, and department from the 2nd table. I have the following sql code to run the search.
$sql = $wpdb->get_results("SELECT * FROM " . $wpdb->prefix .
"dir_personnel where lname like '$search%' or fname like '$search%' or
extension like '$search%' or department=(SELECT id FROM " . $wpdb->prefix .
"dir_departments where department like '$search%') order by lname");
However, the following code runs fine by themselves.
$sql = $wpdb->get_results("SELECT * FROM " . $wpdb->prefix .
"dir_personnel where lname like '$search%' or fname like '$search%' or
extension like '$search%' order by lname");
and
$sql = $wpdb->get_results("SELECT id FROM " . $wpdb->prefix .
"dir_departments where department like '$search%'");
I'm sure it's something simple I'm missing, but any help is appreciated.
I have 2 tables I need to search.
1st table Name: Personnel Fields: id, lname, fname, department, extension
2nd table Name: department Fields: id, department
The 2nd table has the full department name, while the 1st table has the id associated with the department. When the user searches, it should search for the lname, fname, and extension from the 1st table, and department from the 2nd table. I have the following sql code to run the search.
$sql = $wpdb->get_results("SELECT * FROM " . $wpdb->prefix .
"dir_personnel where lname like '$search%' or fname like '$search%' or
extension like '$search%' or department=(SELECT id FROM " . $wpdb->prefix .
"dir_departments where department like '$search%') order by lname");
However, the following code runs fine by themselves.
$sql = $wpdb->get_results("SELECT * FROM " . $wpdb->prefix .
"dir_personnel where lname like '$search%' or fname like '$search%' or
extension like '$search%' order by lname");
and
$sql = $wpdb->get_results("SELECT id FROM " . $wpdb->prefix .
"dir_departments where department like '$search%'");
I'm sure it's something simple I'm missing, but any help is appreciated.
Share Improve this question asked Nov 20, 2018 at 19:55 Darth Mikey DDarth Mikey D 1051 silver badge9 bronze badges1 Answer
Reset to default 0You need to identify the tables for each of the columns that have the same name. You should also be using an IN statement and not = for the department.
I can't test this since I don't have your DB but you can try this:
$sql = $wpdb->get_results("SELECT * FROM " . $wpdb->prefix .
"dir_personnel as personnel where lname like '$search%' or fname like '$search%' or
extension like '$search%' or personnel.department IN (SELECT id FROM " . $wpdb->prefix .
"dir_departments as departments where departments.department like '$search%') order by lname");
I have 2 tables I need to search.
1st table Name: Personnel Fields: id, lname, fname, department, extension
2nd table Name: department Fields: id, department
The 2nd table has the full department name, while the 1st table has the id associated with the department. When the user searches, it should search for the lname, fname, and extension from the 1st table, and department from the 2nd table. I have the following sql code to run the search.
$sql = $wpdb->get_results("SELECT * FROM " . $wpdb->prefix .
"dir_personnel where lname like '$search%' or fname like '$search%' or
extension like '$search%' or department=(SELECT id FROM " . $wpdb->prefix .
"dir_departments where department like '$search%') order by lname");
However, the following code runs fine by themselves.
$sql = $wpdb->get_results("SELECT * FROM " . $wpdb->prefix .
"dir_personnel where lname like '$search%' or fname like '$search%' or
extension like '$search%' order by lname");
and
$sql = $wpdb->get_results("SELECT id FROM " . $wpdb->prefix .
"dir_departments where department like '$search%'");
I'm sure it's something simple I'm missing, but any help is appreciated.
I have 2 tables I need to search.
1st table Name: Personnel Fields: id, lname, fname, department, extension
2nd table Name: department Fields: id, department
The 2nd table has the full department name, while the 1st table has the id associated with the department. When the user searches, it should search for the lname, fname, and extension from the 1st table, and department from the 2nd table. I have the following sql code to run the search.
$sql = $wpdb->get_results("SELECT * FROM " . $wpdb->prefix .
"dir_personnel where lname like '$search%' or fname like '$search%' or
extension like '$search%' or department=(SELECT id FROM " . $wpdb->prefix .
"dir_departments where department like '$search%') order by lname");
However, the following code runs fine by themselves.
$sql = $wpdb->get_results("SELECT * FROM " . $wpdb->prefix .
"dir_personnel where lname like '$search%' or fname like '$search%' or
extension like '$search%' order by lname");
and
$sql = $wpdb->get_results("SELECT id FROM " . $wpdb->prefix .
"dir_departments where department like '$search%'");
I'm sure it's something simple I'm missing, but any help is appreciated.
Share Improve this question asked Nov 20, 2018 at 19:55 Darth Mikey DDarth Mikey D 1051 silver badge9 bronze badges1 Answer
Reset to default 0You need to identify the tables for each of the columns that have the same name. You should also be using an IN statement and not = for the department.
I can't test this since I don't have your DB but you can try this:
$sql = $wpdb->get_results("SELECT * FROM " . $wpdb->prefix .
"dir_personnel as personnel where lname like '$search%' or fname like '$search%' or
extension like '$search%' or personnel.department IN (SELECT id FROM " . $wpdb->prefix .
"dir_departments as departments where departments.department like '$search%') order by lname");
本文标签: queryNested select statements not working
版权声明:本文标题:query - Nested select statements not working 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749165346a2326083.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论