doufendi9063
2016-02-16 12:19
浏览 12
已采纳

在SQL中使用PDO选择列中的一个celd

I have this situation after my SQL stament: One single column with the same result. For example, the age of all people with blue eyes and green jeans, will return me one column "age" with the result for example 50 years (because all people with this characteristics have 50), repeted as many time as there are people with this parameters in the DB.

How I can do in PDO (or SQL) to filter or to get only one result, or only one fild (50)? I try: fetchAll(PDO::FETCH_ASSOC) and fetchColumn() but both return a array or nothing

图片转代码服务由CSDN问答提供 功能建议

我的SQL版本之后出现这种情况:一个列具有相同的结果。 例如,所有蓝眼睛和绿色牛仔裤的人的年龄,将返回一栏“年龄”,结果例如50年(因为所有具有这种特征的人都有50岁),重复的时间与有人一样多 DB中的这个参数。

如何在 PDO (或 SQL )中进行过滤或仅获取一个结果,或仅获得一个结果(50 )? 我尝试: fetchAll(PDO :: FETCH_ASSOC) fetchColumn()但两者都返回一个数组或什么都没有

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

1条回答 默认 最新

  • dongsheng9203 2016-02-16 12:32
    最佳回答

    It's SQL.

    SELECT age FROM people WHERE eyes='blue' AND jeans='green' LIMIT 1
    

    Note that LIMIT part that will limit the results to only one row, which you'll be able to get with PDO's fetchColumn():

    $sql = "SELECT age FROM people WHERE eyes='blue' AND jeans='green' LIMIT 1";
    $age = $pdo->query($sql)->fetchColumn();
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题