W
W
webpauk2013-11-20 15:44:31
MySQL
webpauk, 2013-11-20 15:44:31

Using a Variable as a List of Fields

I want to link 2 tables:

SELECT T1.id, <b>???</b>
FROM sc_user T1 
INNER JOIN sc_user_value T2 ON T1.id_user = T2.id
WHERE T1.ID = 1
LIMIT 1;


The required fields of the second table are stored in the third table, I get the query:
SELECT GROUP_CONCAT(CONCAT('T2.', name) SEPARATOR '+" "+') AS name FROM sc_user_field WHERE use_as_name ORDER BY sort


Query result: T2.last_name+" "+T2.first_name+" "+T2.second_name

Question: how to combine two queries into one?

Answer the question

In order to leave comments, you need to log in

7 answer(s)
F
FanatPHP, 2013-11-20
@FanatPHP

No need to store a list of field names in another table.
It is necessary to read at least a primer on relational databases and learn how to use them humanly.

D
Dinar Garipov, 2013-11-20
@gaaarfild

Never shift business logic to the database. A database is a place where data is stored, not processed. Then your applications will be dependent.

W
Webdesus, 2013-11-20
@Webdesus

You thrust the 2nd request in under request of the first.
Get something like this.

Select t1.*,t2.*,t3.*
from t1
inner join t2 on t2.id=t1.t2id
inner join (select * from t3 ) as t3 on t3.id=t2.t3id

S
Sergey Kazarinov, 2013-11-20
@serkaz

Throw off the structure of the tables, you will get an answer faster.

W
webpauk, 2013-11-20
@webpauk

simplified model:
sc_user
id -> INT
sc_user_value
id -> INT
id_user -> INT (associated with sc_user.id)
user-added fields
sc_user_fields
name - CHAR (stores field name sc_user_value. user-added fields )

W
webpauk, 2013-11-20
@webpauk

simplified model:
sc_user
id -> INT
sc_user_value
id -> INT
id_user -> INT (associated with sc_user.id)
user-added fields
sc_user_fields
name - CHAR (stores field name sc_user_value.user-added fields)

W
webpauk, 2013-11-20
@webpauk

using php, of course, you can split the request into 2 requests, in the first - get the value, and insert it into the second request ...
but ... is there a way to do it in one request!

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question