MySQL Выберите разные значения в одной строке - PullRequest
0 голосов
/ 14 мая 2019

Здравствуйте, у меня возникла проблема, потому что я хочу выбрать 3 разных значения из одной таблицы и отобразить их в разных столбцах в одной строке. Я думаю, что это может быть достигнуто, но я не могу сделать это правильно. Это таблица, из которой я хочу извлечь данные:

select * from mantis_custom_field_string_table limit 20 \G;                                                                   *************************** 1. row ***************************
field_id: 4
  bug_id: 1957
   value: COCO-AB-00132
    text: NULL
*************************** 2. row ***************************
field_id: 3
  bug_id: 1957
   value: COCO-AB-00132-100.220.181.65
    text: NULL
*************************** 3. row ***************************
field_id: 7
  bug_id: 1957
   value: Lore Ipsum Lalala.
    text: NULL

Это первый подход, который я пробую:

select summary,value from mantis_bug_table inner join mantis_custom_field_string_table on mantis_custom_field_string_table.bug_id=mantis_bug_table.id where (field_id=4 or field_id=3 or field_id=7) and id=1957;
+------------------------------------------+---------------------------------------------------------------------------------------------------------+
| summary                                  | value                                                                                                   |
+------------------------------------------+---------------------------------------------------------------------------------------------------------+
| This is the summary | COCO-AB-00132-100.220.181.65                                                                            |
| This is the summary | COCO-AB-00132                                                                                           |
| This is the summary | Lore Ipsum Lalala.     |
+------------------------------------------+-----------------------------------------------------------------------------------------

Вот что я хочу:

+------------------------------------------+---------------------------------------------------------------------------------------------------------+
| summary                                  | ID |      COCO |  Lore                                                                            |             
+------------------------------------------+---------------------------------------------------------------------------------------------------------+
| This is the summary | COCO-AB-00132-100.220.181.65     | COCO-AB-00132    | Lore Ipsum Lalala                                                                  |

И это я тоже пробовал, но безуспешно

mysql> select summary,value as ID, value as COCO, value as Lore  from mantis_bug_table inner join mantis_custom_field_string_table on mantis_custom_field_string_table.bug_id=mantis_bug_table.id where (field_id=4 or field_id=3 or field_id=7) and id=1957 \G;
*************************** 1. row ***************************
summary: This is the summary
     ID: COCO-RT-00132-100.220.181.65
   COCO: COCO-RT-00132-100.220.181.65
   Lore: COCO-RT-00132-100.220.181.65
*************************** 2. row ***************************
summary: This is the summary
     ID: COCO-RT-00132
   COCO: COCO-RT-00132
   Lore: COCO-RT-00132
*************************** 3. row ***************************
summary: This is the summary
     ID: This is the summary.
   COCO: This is the summary.
   Lore: This is the summary.

Я надеюсь, что кто-то может мне помочь с этим!

1 Ответ

0 голосов
/ 14 мая 2019

Если вы знаете, что вы ищете, вы можете использовать внутренний запрос выбора Например

SELECT
(
  SELECT value
    FROM mantis_custom_field_string_table
   WHERE
         "put your clause here"
   LIMIT 1
) AS summary,
(
  SELECT value
    FROM mantis_custom_field_string_table
   WHERE
         "put your clause here"
   LIMIT 1
) AS ID,
(
  SELECT value
    FROM mantis_custom_field_string_table
   WHERE
         "put your clause here"
   LIMIT 1
) AS COCO,
(
  SELECT value
    FROM mantis_custom_field_string_table
   WHERE
         "put your clause here"
   LIMIT 1
) AS LOREM

Важно знать, что каждый из ваших внутренних запросов на выборку должен возвращать ОДИН СТРОК

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...