Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7611275
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T01:37:54+00:00 2026-05-31T01:37:54+00:00

I have 3 tables : wp_users – stores main information about all users, wp_usermeta

  • 0

I have 3 tables :

  • wp_users – stores main information about all users,
  • wp_usermeta – stores additional information about users(first/last name/etc),
  • wp_friends – stores information about friends from third party services related to a specific user from wp_users

If you are not familiar with WordPress, you can see the structure of both tables at http://codex.wordpress.org/images/9/9e/WP3.0-ERD.png

The structure of my custom table wp_friends is as follows:

CREATE TABLE wp_friends (
    id bigint(20) unsigned NOT NULL auto_increment,
    uid bigint(20) unsigned NOT NULL default '0',
    fr_id VARCHAR (60) NOT NULL default '',
    service VARCHAR (20) NOT NULL default '',
    name VARCHAR (80) NOT NULL default '',
    photo VARCHAR (255) NOT NULL default '',
    PRIMARY KEY (id),
    KEY uid (uid),
    KEY fr_id (fr_id),
    KEY service (service)
)`

The uid column is corresponds to the ID column in the wp_users table – this is how I determine which record corresponds to which user.

What I’m trying to do is to create a query that will look in all of the three tables for a match against a keyword. Here is what I’ve come with so far(the first part was generated by a search function of WordPress):

SELECT 
    wp_users.ID,wp_users.display_name,wp_users.user_login,
    wp_users.user_email,fr.fr_id,fr.name,fr.photo,fr.service
    FROM wp_users

    INNER JOIN wp_usermeta ON (wp_users.ID = wp_usermeta.user_id)

    LEFT JOIN wp_socialaccess_friends AS fr ON fr.uid = 2

    WHERE 
    (
        (user_login LIKE '%nik%' OR user_nicename LIKE '%nik%')
        AND 
        (wp_usermeta.meta_key = 'wp_user_level' AND CAST(wp_usermeta.meta_value AS CHAR) != '0')
    )
    OR ( fr.uid = 2 AND (fr.fr_id LIKE '%nik%' OR fr.name LIKE '%nik%'))

    GROUP BY wp_users.ID,fr.fr_id ORDER BY user_login ASC

In the above query, the keyword is “nik”(which also matches a user_login column). The fr.uid part is needed so the returned results are only for the current user. The query fails in the following ways:

  • It returns all rows from the wp_friends table(because the user_login column is matched as well), that have wp_friends.uid = 2
  • It returns rows that have wp_friends.uid = 2 but matched with users where wp_users.ID != 2

Is it possible to create a single query, that would return the selected columns, but will also prevent duplicates?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T01:37:55+00:00Added an answer on May 31, 2026 at 1:37 am

    What about joining on a sub-select like:

    SELECT 
        wp_users.ID,wp_users.display_name,wp_users.user_login,
        wp_users.user_email
        FROM wp_users
    
        INNER JOIN wp_usermeta ON (wp_users.ID = wp_usermeta.user_id)
        left join(
          select fr_id, uid,name,photo,service from wp_socialaccess_friends where
                uid = 2 and
                (fr_id LIKE '%nik%' OR name LIKE '%nik%')
            ) AS fr ON wp_users.ID = fr.uid 
    
        WHERE 
        (
            (user_login LIKE '%nik%' OR user_nicename LIKE '%nik%')
            AND 
            (wp_usermeta.meta_key = 'wp_user_level' AND CAST(wp_usermeta.meta_value AS CHAR) != '0')
        )
    
        GROUP BY wp_users.ID,fr.fr_id ORDER BY user_login ASC
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have 3 tables Links Link ID Link Name GroupID (FK into Groups) SubGroupID
This is how things work I use Wordpress category tables and have 2 main
I have a bunch of WordPress users that I'd like removed in the wp_users
I have 2 tables (Wordpress Database), one which stores posts and the other post
I have tables like this: tblUsers int UserID string UserName tblUsersInRoles int UserID int
In my database I have tables that define types for example Table: Publication Types
When I have tables in my database that have PK/FK relationships (int) and when
Our client's request is to have tables in PDF with rounded corner. I only
Using MS Access 2007, I am creating a student management database. I have tables
I have several tables whose only unique data is a uniqueidentifier (a Guid) column.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.