mysql - 找到链接的图像

时间:2014-04-15 12:57:35

标签: mysql sql select

我有一个存储图像的表格,我想找到' root'图像之间的链接。 例如,这是表格:

+--------+----------+--------------+------------+----------+
| fileID | fileName | fileLinkName | fileLinkID | fileData |
+--------+----------+--------------+------------+----------+
| 1      | file1    | None         | None       | BLOBxxx  |
+--------+----------+--------------+------------+----------+
| 2      | file2    | file1        | 1          | BLOBxxx  |
+--------+----------+--------------+------------+----------+
| 3      | file3    | file2        | 2          | BLOBxxx  |
+--------+----------+--------------+------------+----------+
| 4      | file4    | None         | None       | BLOBxxx  |
+--------+----------+--------------+------------+----------+
| 5      | file5    | file3        | 3          | BLOBxxx  |
+--------+----------+--------------+------------+----------+

fileLinkName是与其关联的fileName的名称,而fileLinkID是与其关联的fileID。图像数据存储在fileData

我需要一个SQL语句,它会显示此表中的所有链接文件。 我需要这样的输出:

    +--------+----------+--------------+------------+----------+
    | fileID | fileName | fileLinkName | fileLinkID | fileData |
    +--------+----------+--------------+------------+----------+
    | 1      | file1    | None         | None       | BLOBxxx  |
    +--------+----------+--------------+------------+----------+
    | 2      | file2    | file1        | 1          | BLOBxxx  |
    +--------+----------+--------------+------------+----------+
    | 3      | file3    | file2        | 2          | BLOBxxx  |
    +--------+----------+--------------+------------+----------+
    | 5      | file5    | file3        | 3          | BLOBxxx  |
    +--------+----------+--------------+------------+----------+

我想找到图像之间的所有链接。

在上面的示例中:

  • file2链接到文件1
  • file3链接到文件2
  • 文件5链接到文件3

2 个答案:

答案 0 :(得分:0)

您可以按如下方式使用自联接:

SELECT CONCAT(a1.fileName,' is linked to file ',a2.fileLinkID)
FROM fileTable a1 
JOIN fileTable a2 ON a1.fileID = a2.fileLinkID 
ORDER BY fileID ASC

答案 1 :(得分:0)

您可以使用以下代码检查字段是否为空:

SELECT * FROM table_name WHERE fileLinkID!= NULL或fileLinkID ='';