You 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");
Related Posts:
- How to display SQL query that ran in query?
- How to Use Wildcards in $wpdb Queries Using $wpdb->get_results & $wpdb->prepare?
- wpdb get posts by taxonomy SQL
- Multipart/formatted MySQL query problem
- WordPress creating excessive joins on meta_query with search
- cron job to auto delete posts of a specific post type older than x days
- Are database queries created using WordPress filters protected from SQL injection?
- Get posts by category with pure SQL query
- Custom query to get terms from post ids
- Get the timout value of a saved transient?
- Select query for a login
- What’s wrong with my $wpdb prepare?
- How do I see the mysql query generated by get_posts( $args )?
- sql select query in wordpress ‘page’ [closed]
- Select Query demand excesive resources [closed]
- problem with sql query in wordpress plugin
- Attempt to improve WP search, can someone check my SQL query?
- wpdb query problem to access previous 3 days posts
- I can’t figure out what’s wrong with this statement. $wpdb->query update
- Custom Query – Based on user input
- how to insert missing tags into the posts through mySQL?
- $wpdb->prepare affecting the query?
- WordPress SQL search, how to handle SQL Injection?
- exclude pingbacks from wordpress SQL query
- prepare function sql safe method
- WordPress SQL query to tag all posts containing a specific word on title
- Select column name dynamically mySQL Query
- How do I query for posts by custom meta and those that have been stickied?
- Change pure SQL database query to WordPress post query?
- Cannot get sql request from Query object?
- I want to get on those users their meta value are like “AGENT” .. but this query is not working
- Display data from phpMyAdmin with WordPress
- SQL to Query the db and return all posts and it’s metas
- SQL query to delete users with multiple meta keys and comments
- Speed up search query that searches in post meta?
- Are there any best practices for creating a Like/Favourite feature in WordPress using custom MySQL tables and without any plugins?
- Fastest and most efficient SQL query to check if UID exists
- How to make MySQL search queries with quotes
- How i make a custom sql query for Woocommerce
- $wpdb query for price in custom field value
- Run an update query in a function
- How to display a specific category using a custom Query in WordPress?
- Slow query when joining wp_posts with a lookup-table?
- Wpdb generates too many queries
- learn to run wpdb class
- SQL Query : how copy all tags of post into their post content in wordpress by sql query
- Where can I find the SQL to get the most used information by wordpress database?
- Custom Query for wp_posts using wp_postmeta
- 3 queries to update WordPress
- ORA-01843 not a valid month- Comparing Dates
- Postgresql column reference “id” is ambiguous
- Difference between fragment caching and wp_cache
- Select User by Joining Multiple Meta Value Results
- You have an error in your SQL syntax – Help with query
- WordPress Query orderby title with UTF8 string in title
- Searching With Apostrophe
- List author’s posts with SQL
- Where is the query in wp-login.php
- Order posts by recent comments
- query if page has not child
- get_posts showing only 5 posts. (nopaging and posts_per_page -1 not working)
- Order by title – but now built in wordpress func, sort the_title
- Display posts between two particular dates
- pre saved posts query from db options table
- Order posts by date, but also give priority for a specific term
- $post->ID not working in combination with a custom query
- Custom Query returns array, but I can’t see it’s elements
- How to insert Collate into WordPress search query?
- Combine “Exclude” Query in functions.php
- Optimize slow SQL query for multiple meta values
- Custom select query two tables by a meta key
- Wrap/close ‘query’ for a first-letter ordered listing of posts
- wpdb query with dynamic column name?
- How do you display the number of a post in a query?
- How to return count of custom post type posts with a specific custom field value via $wpdb?
- How can I get the name of term post meta value which equals term id
- Help with Related Posts Function
- Export all posts to import as product items
- Trying to use WP_Query to return post with and without meta values
- get products by custom date variable
- How to delete all images from code in all post_content
- How to sort by two meta keys in admin area?
- Restrict WordPress search to a single ACF field
- wp query multiple values > display a specific value first
- PHP Fatal error: Uncaught Error: Call to a member function insert() on null
- OR condition not working
- Query not returning child posts for any parent post ID
- Use $wpdb->get_results with filter based on array
- Add Hook for clearing transient when post is added
- Show links to child pages on both parent AND child pages
- Special Query: Title, Terms, Content – %LIKE%
- Modify Global Posts Plugin
- How to reference same column name but different table in custom db query
- Retrieving custom field array value through db query
- How do I troubleshoot Maximum execution time of 60 seconds exceeded in updating a Media File?
- update $wpdb one query
- Admin Post List table Query filtering “WHERE” for custom post type
- How to use a dynamic term id for the query block
- Remove posts that start with similar words (like a delete duplicate posts plugin)
- Help with query