如何在Python中合并SQLite中的两列?

0 投票
2 回答
1389 浏览
提问于 2025-04-16 14:09

我该如何在sqlite3中合并两列?我知道可以在select语句中使用+,但是当我在Python中尝试这段代码时:

select first+last as name from users 

结果却是0

我还尝试了这个语句:

select first||" "||last as name from users

但是出现了错误。

我想把名字和姓氏显示在一列中,像这样:

'tara.panagop@where.com', 'tara panagop', 

2 个回答

0

如果你有空的行(也就是没有数据的行),你可能想试试这个

select (coalesce(first,'')||" "||coalesce(last,'')) as name from users
0

as 的优先级比 || 高,所以这里需要加括号:

select (first||" "||last) as name from users

在引号中使用 " 时,要用 \" 来转义:

cur.execute("select (first||\" \"||last) as name from users")

撰写回答