如果我有一个带有主键'ID'的表,然后我在列中有其他数据,(我只想说例如:Name,Address,PhoneNum)如何进行查询以便显示名称,地址和PhoneNum(整行)垂直为特定ID?
如果需要,我可以张贴一张图片来解释我的意思。
谢谢! :)
答案 0 :(得分:1)
对于名为[Clients]
的表格中的样本数据ID LastName FirstName Email
-- -------- --------- -----
1 Thompson Gord gord@example.com
2 Loblaw Bob bob@example.com
3 Kingsley Hank hank@example.com
查询
SELECT "LastName" AS fld, LastName AS val FROM Clients WHERE ID=1
UNION ALL
SELECT "FirstName" AS fld, FirstName AS val FROM Clients WHERE ID=1
UNION ALL
SELECT "Email" AS fld, Email AS val FROM Clients WHERE ID=1;
返回
fld val
--- ---
LastName Thompson
FirstName Gord
Email gord@example.com