2011-12-29 12 views
3

テーブルの同じ列に格納されている2つのデータを抽出して2つの列に表示します。私は多くの検索を行い、多くのテストを行いますが、何も動作しません。 行うのは簡単かもしれない...Mysqlは2つの列を選択して表示します

これが私のテーブルです:

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

私がすることによって別のものに1つのフィールドと経度(FIELD_ID = 66)で緯度(FIELD_ID = 65)を表示したいですuser_idで結合します。選択

2:私は2つの列で一つのテーブルにこのデータを表示するにはどうすればよい

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は、経度のIDとどのように関連していますか? –

答えて

2

Bassamの答えはすべてが順調と良いですが、それはまさに、世界で最も効率的なものではありません。

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

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

+0

うれしいことに、私は –

+0

完了したと推測しなければならなかったので、おそらくあなたはあなたがuser_idに参加していると言う質問を編集するべきです!再度、感謝します。 – Arystark

関連する問題