English 中文(简体)
MySQL:与有多个子关系的Joins取得的结果
原标题:MySQL: Limit Results related to FROM with Joins that have multiple subelements

我的一张主片,一是从一伙中选择的子里挑选的,一是一张桌子。 例:

person         skill           person_to_skill
id  | name     id | skill      id | p_id | s_id
------------   ------------    ----------------
1   | jim      1  | sewing     1  | 1    | 2
2   | peter    2  | cooking    2  | 2    | 1
3   | susan    3  | singing    3  | 2    | 3
4   | kevin                    4  | 3    | 1
                               5  | 3    | 2
                               6  | 4    | 3

So now we see, sim has only one skill, peter has two and so forth. Now if i select from person, koin skill and then also join person_to_skill, but i only want two persons. How do i manage to do so without grouping and thereby not getting all the skills?

简言之,我要从具有所有技能的“人”中挑选两人。

I tried just using LIMIT but that limits the result rows, not the persons. If i use GROUP BY i only get one skill per person. Is this possible without a subselect?

是否有任何想法?

My Approach so far, changed to work with the example, looks like this:

SELECT p.id,p.name,s.skill
FROM person AS p
LEFT JOIN person_to_skill psk ON (psk.p_id = p.id)
LEFT JOIN skill s ON (s.id = psk.s_id)
ORDER BY p.name
LIMIT 0,2
最佳回答

Limit number of persons at very beginning in subquery then join to them other tables as you ve already done:

SELECT p.id,p.name,s.skill
FROM (select * from person ORDER BY name LIMIT 0,2) AS p
LEFT JOIN person_to_skill psk ON (psk.p_id = p.id)
LEFT JOIN skill s ON (s.id = psk.s_id)

If you really can t use subqueries you can do it using two queries. Firstly select users ids:

select id from person ORDER BY name LIMIT 0,2

并且随后在接下来的询问中使用这些孩子:

SELECT p.id,p.name,s.skill
FROM person p
LEFT JOIN person_to_skill psk ON (psk.p_id = p.id)
LEFT JOIN skill s ON (s.id = psk.s_id)
WHERE p.id IN (ids from previous query)
问题回答

你可以做这样的事情。

SELECT p.id, p.name, group_concat(s.skill separator  , )

and then group by person and limit the number of rows.





相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签