I am running an SQL statement:
$sql = "SELECT pic, userid
FROM user_details
INNER JOIN wp_users ON user_details.userid
WHERE wp_users.id = user_details.userid
LIMIT $recordstart, $pagesize
";
The query is meant to select records from user_details and wp_users, but should only select if there is a match (wp_users.id = user_details.userid).
The target is for it to basically select from a table while retrieving a record (based on the id match) from the other table
The problem is that it shows duplicate records. How can I fix this problem?
You should put your JOIN condition after ON. Like this:
But the real problem is that you have more then 1 record in
user_detailsfor each corresponding record inwp_users(or vise versa).INNER JOIN is causing database to make a Cartesian product of
user_detailsandwp_usersrecords. Then result table is filtered by your current WHERE condition (wp_users.id=user_details.userid). Depends on your needs you can use GROUP BY or DISTINCT if you want to retrieve unique records only.For example, if you need
useridto be unique: