Results 1 to 4 of 4

Thread: Problem with construction of a query

  1. #1
    Join Date
    Aug 2009
    Posts
    59

    Problem with construction of a query

    I find it hard to build a query. Knowing the user's nickname, I want to display the name of their group assignment (attribute name of the table groups). The id attribute of the table groups is related with attribute id_object table users_groups.

    I thought about this query:

    select name from groups where id = (select id_group from users_groups where id_object = (select id_user from users where nickname = "foo");

    But is there not something cleaner and simpler than subqueries?

  2. #2
    Join Date
    May 2008
    Posts
    2,297

    Re: Problem with construction of a query

    It is simpler:
    SELECT name FROM groups, users, user_groups WHERE id = id_groups AND id_object = id_user AND nickname LIKE "foo";

    If the three tables are in the same basis, otherwise you must use the "inner join"

  3. #3
    Join Date
    Aug 2009
    Posts
    59

    Re: Problem with construction of a query

    To explain the problem, I modified the post in the unique identifier of the table users. Thus, id became id_user. Now I work on a relational database schema that I can not change. The query advised to me logically generates this error message:

    #1052 - Column 'id' in where clause is ambiguous

  4. #4
    Join Date
    Nov 2008
    Posts
    1,221

    Re: Problem with construction of a query

    Add the table name like this:

    SELECT name FROM groups, users, user_groups WHERE groups.id = users_groups.id_groups AND users_groups.id_object = users.id_user AND nickname LIKE "foo";

    You can also give a name to each table in the FROM clause and use something like this for simplicity:

    SELECT name FROM groups g, users u, user_groups ug WHERE g.id = ug.id_groups AND ug.id_object = u.id_user AND nickname LIKE "foo";

Similar Threads

  1. Query cache denied problem
    By Alibamu in forum Software Development
    Replies: 6
    Last Post: 19-06-2010, 12:35 AM
  2. Regex problem in construction
    By Miles Runner in forum Software Development
    Replies: 5
    Last Post: 04-02-2010, 03:59 AM
  3. Problem of nested query
    By MAGAR in forum Software Development
    Replies: 4
    Last Post: 09-12-2009, 09:55 PM
  4. Coldfusion query problem
    By AZUL in forum Software Development
    Replies: 3
    Last Post: 26-11-2009, 07:55 PM
  5. Problem of SQL query
    By Sonic in forum Software Development
    Replies: 5
    Last Post: 18-12-2008, 12:28 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,946,950.90054 seconds with 16 queries