开发者

Selecting table using case clause in from clause

开发者 https://www.devze.com 2023-01-14 14:45 出处:网络
I have 8 table that contain different specific value for computer peripheral they are glpi_device_ram, glpi_device_hdd, glpi_device_gfxcard, glpi_device_sndcard. Each table has the same designation co

I have 8 table that contain different specific value for computer peripheral they are glpi_device_ram, glpi_device_hdd, glpi_device_gfxcard, glpi_device_sndcard. Each table has the same designation column in each table that contain device name. i have table glpi_computer_device that contain FK_device that contain id for each 8 table above and device type column that help me decide which table(from 8 table above) should i associate with FK_device to get designation column. I have create sql syntax but not work. I need to know whether selecting table in FROM clause using CASE clause is allowed? Here is my code

 SELECT CASE device_type
          WHEN "1" THEN "Casing"
          WHEN "2" THEN "Processor"
          WHEN "3" THEN "RAM"
          WHEN "4" THEN "Harddisk"
          WHEN "5" THEN "Network Card"
          WHEN "6" THEN "Drive"
          WHEN "7" THEN "UNKNOWN"
          WHEN "8" THEN "Graphic Card"
          WHEN "9" THEN "Sound Card"
          WHEN "10" THEN "Other device"
        END AS devicetype, 
        CASE device_type
          WHEN "1" THEN "--"
          WHEN "2" THEN "Frequency"
          WHEN "3" THEN "Size"
          WHEN "4" THEN "Capacity"
          WHEN "5" THEN "Mac Address"
          WHEN "6" THEN "Memory Size"
          WHEN "7" THEN "--"
          WHEN "8" THEN "Memory Size"
          WHEN "9" THEN "--"
          WHEN "10" THEN "--"
        END AS secificity_type,
        specificity, 
        (SELECT c.designation
           FROM (SELECT CASE cd.device_type
                          WHEN "2" THEN "glpi_device_processor"
                          WHEN "3" THEN "glpi_device_ram"
                开发者_如何学JAVA          WHEN "4" THEN "glpi_device_hdd"
                          WHEN "5" THEN "glpi_device_iface"
                          WHEN "6" THEN "glpi_device_drive"
                          WHEN "8" THEN "glpi_device_gfxcard"
                          WHEN "9" THEN "glpi_device_sndcard"
                          WHEN "10" THEN "glpi_device_pci"
                        END
                   FROM glpi_computer_device cd 
                  WHERE cd.ID = ID ) AS c
          WHERE c.ID=FK_device)
  FROM `glpi_computer_device` 
 WHERE FK_computers = 1


no that does not work. you should structure this as a UNION of all 8 tables - then wrap that with another select statement to get the value.

0

精彩评论

暂无评论...
验证码 换一张
取 消