SQL - 我可以在每列添加单词吗?

时间:2015-07-08 06:35:58

标签: mysql

我试图在mysql的循环或其他任何内容中添加临时单词?当我尝试select full_name from players时。

我可以做类似的事情,例如:

+--------------+
|  full_name   |
+--------------+
| Ahmed Sayed  |
+----+------+--+
| **ANY WORD** |
+--------------+
| Kareem Gaber |
+------+-------+
| **ANY WORD** |
+------+-------+
| omar galal   |
+--------------+
| **ANY WORD** |
+------+-------+
| Mohamed gad  |  
+------+-------+
| **ANY WORD** |
+--------------+

1 个答案:

答案 0 :(得分:1)

您可以通过自己加入表来实现。 我已经为您做了一个示例:(如果您想尝试我的代码并在表中重复使用,可以使用此测试表。)

CREATE TABLE test
    (`id` int, `productName` varchar(7), `description` varchar(55))
;

INSERT INTO test
    (`id`, `productName`, `description`)
VALUES
    (1, 'OpenIDM', 'Platform for building enterprise provisioning solutions'),
    (2, 'OpenAM', 'Full-featured access management'),
    (3, 'OpenDJ', 'Robust LDAP server for Java')
;

查询:

    SELECT
     CASE
     WHEN query = '1' THEN productName
     ELSE "ANY WORD"
     END AS name
     FROM (
    SELECT 1 AS query, t1.* FROM test AS  t1
    UNION
    SELECT 2 AS query, t2.* FROM test AS t2
)AS t3 ORDER BY id, query;

如果不清楚,请询问您需要的任何内容。

不确定如何在sql小提琴中保存查询,但您可以将其粘贴到此处:http://sqlfiddle.com/#!9/8f530/1