2011-12-29 60 views
3

我想提取存储在我的表的同一列中的两种类型的数据,并将其显示在两列中。我做了很多搜索并做了很多测试,但没有任何工作。 这可能是很容易做到......Mysql选择并显示两列

这是我的表:

| id | field_id | user_id | value   | 
| 175 |  65 |  3 | 48.8984188201264 | 
| 173 |  65 |  2 | 37.9841493  | 
| 177 |  65 |  4 | 48.8440603  | 
| 179 |  65 |  5 | 48.8407529  | 
| 174 |  66 |  2 | 23.7279843  | 
| 176 |  66 |  3 | 2.25568230749569 | 
| 178 |  66 |  4 | 2.3730525  | 
| 180 |  66 |  5 | 2.3213214  | 

我想在一个又一个通过显示在一个字段和经度(FIELD_ID = 66),纬度(FIELD_ID = 65)加入user_id。

两个选择:

select value as longitude 
from wp_bp_xprofile_data 
where field_id=66; 

select value as latitude 
from wp_bp_xprofile_data 
where field_id=65; 

| longitude  | 
| 23.7279843  | 
| 2.25568230749569 | 
| 2.3730525  | 
| 2.3213214  | 

| latitude   | 
| 48.8984188201264 | 
| 37.9841493  | 
| 48.8440603  | 
| 48.8407529  | 

我怎么能在一个表中有两个栏显示这个数据?

感谢

+0

纬度相关的id是经度+ 1的ID? – 2011-12-29 17:45:19

回答

2

巴萨姆的答案是一切都很好,但它不完全是世界上最有效的事情。您可以简化您的查询,像这样:

select 
    t1.user_id, 
    t1.value as latitude, 
    t2.value as longitude 
from 
    wp_bp_xprofile_data t1 
    inner join wp_bp_xprofile_data t2 on 
     t1.user_id = t2.user_id 
where 
    t1.field_id = 65 
    and t2.field_id = 66 

这样,你不拉子查询和您的查询是更清晰一点,至少对我来说。

+0

事实上,这很容易理解。也谢谢你。 – Arystark 2011-12-30 15:33:34

2
SELECT t1.user_id, latitude, longitude 
FROM 
(SELECT user_id, value as latitude 
FROM wp_bp_xprofile_data 
WHERE field_id = 65) t1 
INNER JOIN 
(SELECT user_id, value as longitude 
FROM wp_bp_xprofile_data 
WHERE field_id = 66) t2 ON t1.user_id = t2.user_id 
+0

非常感谢!这正是我想要的结果。 – Arystark 2011-12-30 12:06:33

+0

很高兴提供帮助,你可能应该编辑你的问题,说你加入了user_id,因为我不得不猜测 – 2011-12-30 13:28:23

+0

完成!再次感谢。 – Arystark 2011-12-30 15:24:03