代码之家  ›  专栏  ›  技术社区  ›  Kenshinh

如何在SQL中组合两列,然后根据组合结果选择行数据[duplicate]

  •  -1
  • Kenshinh  · 技术社区  · 4 年前

    我有一个简单的数据库,其中存储了一个分为两列的令牌, token_prefix 作为一根弦 token_id 作为int。我想将这两列组合在一起,并基于组合结果选择行数据。

    table: tokens
    | token_prefix | token_id |
    | ------------ | -------- |
    | 1MXMAS       | 1        |
    | 1MSEED       | 2        |
    

    1MXMAS1

    到目前为止我试过的是,

    $query = $pdo->prepare('SELECT count(*) concat(token_prefix, token_id) as token from tokens where token = ?');
    $query->execute(array($token));
    $number_of_rows = $query->fetchColumn();
    

    1 回复  |  直到 4 年前
        1
  •  0
  •   Nick SamSmith1986    4 年前

    你有几个问题;首先,你在后面少了一个逗号 count(*) 其次,不能在列中使用列别名 WHERE 条款。既然你不需要 token 作为查询的输出,可以通过移动 哪里 条款:

    $query = $pdo->prepare('SELECT count(*) FROM tokens WHERE CONCAT(token_prefix, token_id) = ?');