1

I'm trying to select assets from RT's database with the the values for a set of custom fields as tables. Relevant tables are as follows:

mysql> describe AT_Assets;
+---------------+--------------+------+-----+---------+----------------+
| Field         | Type         | Null | Key | Default | Extra          |
+---------------+--------------+------+-----+---------+----------------+
| id            | int(11)      | NO   | PRI | NULL    | auto_increment |
| Type          | int(11)      | NO   | MUL | 0       |                |
| Name          | varchar(200) | NO   | MUL | NULL    |                |
| Description   | varchar(255) | YES  |     | NULL    |                |
| Status        | varchar(20)  | YES  |     | NULL    |                |
| URI           | varchar(255) | YES  |     | NULL    |                |
| LastUpdatedBy | int(11)      | NO   |     | 0       |                |
| LastUpdated   | datetime     | YES  |     | NULL    |                |
| Creator       | int(11)      | NO   |     | 0       |                |
| Created       | datetime     | YES  |     | NULL    |                |
+---------------+--------------+------+-----+---------+----------------+
10 rows in set (0.00 sec)


mysql> describe CustomFields;
+---------------+--------------+------+-----+---------+----------------+
| Field         | Type         | Null | Key | Default | Extra          |
+---------------+--------------+------+-----+---------+----------------+
| id            | int(11)      | NO   | PRI | NULL    | auto_increment |
| Name          | varchar(200) | YES  |     | NULL    |                |
| Type          | varchar(200) | YES  |     | NULL    |                |
| RenderType    | varchar(64)  | YES  |     | NULL    |                |
| MaxValues     | int(11)      | YES  |     | NULL    |                |
| Pattern       | text         | YES  |     | NULL    |                |
| Repeated      | smallint(6)  | NO   |     | 0       |                |
| BasedOn       | int(11)      | YES  |     | NULL    |                |
| ValuesClass   | varchar(64)  | YES  |     | NULL    |                |
| Description   | varchar(255) | YES  |     | NULL    |                |
| SortOrder     | int(11)      | NO   |     | 0       |                |
| LookupType    | varchar(255) | NO   |     | NULL    |                |
| Creator       | int(11)      | NO   |     | 0       |                |
| Created       | datetime     | YES  |     | NULL    |                |
| LastUpdatedBy | int(11)      | NO   |     | 0       |                |
| LastUpdated   | datetime     | YES  |     | NULL    |                |
| Disabled      | smallint(6)  | NO   |     | 0       |                |
+---------------+-------------+------+-----+---------+----------------+
17 rows in set (0.00 sec)

mysql> describe ObjectCustomFieldValues;
+-----------------+--------------+------+-----+---------+----------------+
| Field           | Type         | Null | Key | Default | Extra          |
+-----------------+--------------+------+-----+---------+----------------+
| id              | int(11)      | NO   | PRI | NULL    | auto_increment |
| CustomField     | int(11)      | NO   | MUL | NULL    |                |
| ObjectType      | varchar(255) | NO   |     | NULL    |                |
| ObjectId        | int(11)      | NO   |     | NULL    |                |
| SortOrder       | int(11)      | NO   |     | 0       |                |
| Content         | varchar(255) | YES  | MUL | NULL    |                |
| LargeContent    | longblob     | YES  |     | NULL    |                |
| ContentType     | varchar(80)  | YES  |     | NULL    |                |
| ContentEncoding | varchar(80)  | YES  |     | NULL    |                |
| Creator         | int(11)      | NO   |     | 0       |                |
| Created         | datetime     | YES  |     | NULL    |                |
| LastUpdatedBy   | int(11)      | NO   |     | 0       |                |
| LastUpdated     | datetime     | YES  |     | NULL    |                |
| Disabled        | smallint(6)  | NO   |     | 0       |                |
+-----------------+--------------+------+-----+---------+----------------+

This query successfully gets me a listing of all assets, but repeats the asset in a separate column for each custom field value:

SELECT AT_Assets.Name, AT_Assets.description, CustomFields.Name, ObjectCustomFieldValues.content FROM ObjectCustomFieldValues inner join CustomFields on ObjectCustomFieldValues.CustomField = CustomFields.id inner join AT_Assets on AT_Assets.id = ObjectCustomFieldValues.ObjectID order by AT_Assets.description;

So I did some reading, and learned how to pivot. Now I have this:

SELECT at_assets.name, 
   at_assets.description AS "Asset Tag", 
   Max(CASE 
         WHEN customfields.name = "make" THEN 
         objectcustomfieldvalues.content 
       END)              AS "Make", 
   Max(CASE 
         WHEN customfields.name = "model" THEN 
         objectcustomfieldvalues.content 
       END)              AS "Model", 
   Max(CASE 
         WHEN customfields.name = "primary user" THEN 
         objectcustomfieldvalues.content 
       END)              AS "Primary User", 
   Max(CASE 
         WHEN customfields.name = "hostname" THEN 
         objectcustomfieldvalues.content 
       END)              AS "Hostname", 
   Max(CASE 
         WHEN customfields.name = "os" THEN objectcustomfieldvalues.content 
       END)              AS "OS", 
   Max(CASE 
         WHEN customfields.name = "purchase date (if known)" THEN 
         objectcustomfieldvalues.content 
       END)              AS "Purchase Date" 
FROM   objectcustomfieldvalues 
   INNER JOIN customfields 
           ON objectcustomfieldvalues.customfield = customfields.id 
   INNER JOIN at_assets 
           ON at_assets.id = objectcustomfieldvalues.objectid 
WHERE  at_assets.id = 5 
ORDER  BY at_assets.description; 

Which works great, when I'm specifying a single asset. However, I would like this to run across every asset. Otherwise I need a Python script to run this hundreds of times manually incrementing the asset id, which is fairly inelegant. How do I go about getting a full list?

4

2 回答 2

1

您了解了有关枢轴的所有内容,除了添加group by.

在 之后添加以下行where

group by at_assets.name, at_assets.description

要查看多行,请删除或调整where子句。

您的查询发生的情况是 MySQL 将其识别为聚合查询,因为它使用MAX(). 没有group by,所以它产生一行——所有行的聚合。

变量at_assets.name和子句at_assets.description中的情况如何?select你可能会问。好吧,大多数 SQL 引擎都会犹豫并产生错误。这些变量既不在聚合函数中,group by也不是聚合函数的参数。MySQL 有一个(错误)功能,称为隐藏列,允许此类引用。但是,这些值来自源数据中的任意行,因此除非组中的所有值都相同,否则该值没有意义。

于 2013-07-29T20:24:07.087 回答
0

你只需要group by在这里。并且不指定资产名称。

改变这个

  WHERE  at_assets.id = 5 
  ORDER  BY at_assets.description; 

   group by at_assets.name
   ORDER  BY at_assets.description; 
于 2013-07-29T20:24:43.273 回答