Search code examples
phpmysqlsqlzend-frameworkzend-db

Mysql/sql/zend query with one select?


Well so I got a tiny problem, I got a db and 2 tables like this

Table User
user_id

Table UserrToData
user_id int pk
data_id int

Table UserData
data_id
name
nick

I got the user_id and i need to get records from the userdata , is it possible using 1 query?

Of course, if anyone know how I would really appreciate if he would help me : )

For the selecting I got only the relationship user_id > data_id

update

Guys I got huge db I just simplified the problem to the minimum ^_-


Solution

  • Hope this helps:

    SELECT name, nick
    FROM  UserToData utd INNER JOIN UserData ud ON utd.data_id = ud.data_id
    WHERE utd.user_id = [supplied userid]
    

    With such little data, however, there is no need for separate tables, just store name and nick in the User table:

    Table User
    user_id pk
    name
    nick