0
I have two tables, one for users and one for photos where each line contains the user id of the respective photo.
I want to select the user data and grab the photos by the user id of the photos table.
In this way it returns me the duplicated data from the user table because the user contains several photos in the photo table.
How can I only get once the user data(user table) and all the photos to which it belong from the photos(photo table), which the user(user_id
) contains in the table photos?
$sql = "SELECT u.id, u.username, u.genero, u.idade, u.local, u.descricao, p.user_id, p.location
FROM user AS u
INNER JOIN photos AS p
ON u.id=p.user_id
";
David you already tried to use "select distinct"
– krispim
already yes, but the return is the same
– David Concha
But you want the pictures, right? If you want the pictures and you need their location
p.local
then when doing Join you will have to have user data for each line. Think about the real world, each photo has a user, so each SQL line comes a different picture and each one has a user, only in your case the user is the same...– Ricardo
I understood that he didn’t want the photos, but the quantity. I understood this by this phrase removed from the question: "and the totality of the photos (table photo) that the user(user_id) contains in the table photos".
– cantoni
@Cantoni, I noticed that too, but I also saw the
p.local
There, I decided to leave the comment to see which of the two things he really needs. In case the whole he meant could be all the photos and not a Count... Since you had already put Count in the answer I only talked about the case ofp.local
– Ricardo
@Ricardo only wanted the user’s information and all the user’s own photos. Only photos can be repeated because the user can have multiple photos.
– David Concha
David, do you want the total photo only, type, user david has 50 photos or want the details of all photos?
– Ricardo
the
p.location
is the directory of the photos, no problem. they are displayed with the query q I have. only for each photo line it duplicates the user data– David Concha
@I want to receive the user information and all the photos that belong to him. This query returns duplicate user information because it has 2 photos, for example. If the user has 4 photos, see the 4 photos plus 4x the username, for example.
– David Concha