Question

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 ^_-

Was it helpful?

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

OTHER TIPS

You should overthink your database design. The highest aim is always to prevent redundancies.

If you want the user to have more than one userdata entry you should define your database like this:

Table User
user_id pk

Table UserData
data_id pk
user_id fk
name
nick

So the query would be SELECT * FROM UserData WHERE user_id = ?.

If you only want the user to have one set of userdata you should integrate it into the user table:

Table User
user_id pk
name
nick

So the query would be SELECT * FROM User WHERE user_id = ?.

Why have you put the name and nick in a separate table? You can just put the data in one user table. Might be wise to learn more about how to formulate entities and relations in your database.

Either way, if you ever need to do something like this in Zend at a later time, you can use Zend_Db_Select and do a join. Lookie lookie: http://framework.zend.com/manual/1.12/en/zend.db.select.html#zend.db.select.building.join

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top