我已经使用了stackoverflow上提出的所有解决方案来解决我的问题,但它仍未解决:
我需要获得由plan_id订购的出价用户而不会重复。这是我使用的查询:
SELECT DISTINCT b.*, u.username, u.name, s.plan_id, s.user_id FROM #__jb_bid b
"INNER JOIN #__users u ON b.user_id=u.id ".
"INNER JOIN #__jb_plan_subscr s ON b.user_id=s.user_id ".
"WHERE b.project_id =".$id."".
"ORDER BY s.plan_id";
我正在使用Joomla 3和个性化组件,我有这样的结构:
------------------------------------
|id | user_id | project_id |
|-----------------------------------
|1 | 22 | 1 |
|2 | 24 | 1 |
|3 | 23 | 1 |
|4 | 25 | 1 |
|5 | 26 | 2 |
------------------------------------
------------------------------------
|id | username | name |
|-----------------------------------
|22 | username22 | name22 |
|23 | username23 | name23 |
|24 | username24 | name24 |
|25 | username25 | name25 |
|26 | username26 | name26 |
|27 | username27 | name27 |
------------------------------------
------------------------------------
|id | user_id | plan_id |
|-----------------------------------
|1 | 22 | 1 |
|2 | 22 | 2 |
|3 | 23 | 1 |
|4 | 24 | 1 |
|5 | 25 | 1 |
------------------------------------
查询结果为:
username22
username24
username25
username22
username23
由于
答案 0 :(得分:0)
<强> SQL Fiddle Demo 强>
SELECT b.*, u.username, u.name, s.plan_id, s.user_id
FROM (SELECT `user_id`, MAX(`plan_id`) `plan_id`
FROM __jb_plan_subscr
GROUP BY `user_id`
) s
INNER JOIN __jb_bid b
ON b.user_id = s.user_id
INNER JOIN __jb_users u
ON b.user_id = u.id
WHERE project_id = 1
ORDER BY s.plan_id;
<强>输出强>
| id | user_id | project_id | username | name | plan_id | user_id |
|----|---------|------------|-----------|-------|---------|---------|
| 2 | 24 | 1 | username3 | name3 | 1 | 24 |
| 1 | 22 | 1 | username1 | name1 | 2 | 22 |