0

Given info about an object: id, user_id, group_id

Given info about an user: id_user, id_loc

I need to get i one query:

  • The name of the user (in table users)

  • The name of the location of the user (in table locs)

  • The name of the group of the object (in table groups)

I am trying like this:

SELECT usuarios.first_name as username
    , usuarios.id as userid
    , usuarios.avatar as useravatar
    , usuarios.id_loc
    , locs.name as locname
    , groups.name as groupname 
FROM usuarios,groups,locs 
WHRE usuarios.id_loc = locs.id 
    AND usuarios.id = 1 
    AND group.id = LIMIT 1

having an error saying

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'AND locs.id = 3 LIMIT 1' at line 3

What am i doing wrong? can i do this in one query?

-EDIT-

This is the query generator code (php+mysql):

$query_loc_group_user = 'SELECT usuarios.first_name as username,
                                            usuarios.id as userid,
                                            usuarios.avatar as useravatar,
                                            usuarios.id_loc, 
                                            locs.name as locname,
                                            groups.name as groupname
                                     FROM   usuarios,groups,locs
                                     WHRE   usuarios.id_loc = locs.id
                                     AND    usuarios.id = '.$this->id_user.'
                                     AND    group.id = '.$this->id_group.'
                                     LIMIT 1';

In case it helps, i am trying to do in one query this

function get_info(){
    $info;
    $result = cache_query('SELECT first_name,last_name,avatar FROM   usuarios WHERE  id = '.$this->id_user);
    foreach($result as $extra){
        $info['username'] = $extra['first_name'].' '.$extra['last_name'];
        $info['avatar'] = $extra['avatar'];
    }
    $result1 = cache_query('SELECT name FROM locs WHERE  id = '.$this->id_user);
    foreach($result1 as $extra){
        $info['locname'] = $extra['name'];
    }   
    $result2 = cache_query('SELECT name FROM groups WHERE  id = '.$this->id; /*objectid*/
    foreach($result2 as $extra){
        $info['groupname'] = $extra['name'];
    }
    return $info;
}
Taryn
  • 242,637
  • 56
  • 362
  • 405
Toni Michel Caubet
  • 19,333
  • 56
  • 202
  • 378

3 Answers3

0

What about this?

$sql = 'SELECT usuarios.name as username,usuarios.avatar as useravatar, usuarios.id_loc, locs.name as locname';
$sql .= ' FROM usuarios, groups, locs';
$sql .= ' WHERE usuarios.id = '.$this->id_user.' AND groups.id = '.$this->id_group.' AND locs.id = '.$this->id_loc.' LIMIT 1';

$res = mysql_query($sql) or die(mysql_error());
Sebas
  • 21,192
  • 9
  • 55
  • 109
0

You need something to relate usuarios or locs to groups. I don't know your schema, but it will probably be something like this:

SELECT
    usuarios.first_name as username,
    usuarios.id as userid,
    usuarios.avatar as useravatar,
    usuarios.id_loc,
    locs.name as locname,
    groups.name as groupname
FROM
    usuarios,
    groups,
    locs
WHRE
    usuarios.id_loc = locs.id AND
    [something?] = groups.id AND    -- This is what you're missing.
                                    -- What table/column relates to groups?
    usuarios.id = 1 AND
    group.id = 1
LIMIT 1

...where [something?] is probably an id_group column in either usuarios or locs. Right now, your top query has two errors. The first is a simple syntax error, you don't specify a group.id value. The second one, though, which is more serious, is that you don't have a join clause for your groups table.

If you post your schemas for the three tables, I can probably give you an even better answer, but I'm betting that's what you're missing. If you don't have a group_id column in usuarios or locs, then you need to be more clear about what exactly you're looking to query, and some example data might be helpful with what your expected result is.

King Skippus
  • 3,801
  • 1
  • 24
  • 24
0

well after some help friend i got to this solution

SELECT usuarios.first_name,usuarios.last_name,usuarios.avatar,poblacion.poblacion,groups.name as groupname
                                FROM usuarios LEFT JOIN poblacion ON usuarios.id_loc=poblacion.idpoblacion,
                                     helps LEFT JOIN groups ON helps.id_group = groups.id
                                WHERE  usuarios.id = '.$this->id_user.' AND groups.id = '.$this->id_group
Toni Michel Caubet
  • 19,333
  • 56
  • 202
  • 378