【问题标题】:Select multiple records by short query通过短查询选择多条记录
【发布时间】:2012-12-29 11:27:59
【问题描述】:
function comp_post_code($comp_post_code){
global $host, $dbname, $user, $pass;
$DBH = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass);
$DBH->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION); ///*** for error handling
$STH = $DBH->prepare("SELECT * from uk_data where 
comp_post_code like :comp_post_code and cat1 like :cat");
$STH->bindValue(':cat', "%$cat1%", PDO::PARAM_STR);
$STH->bindValue(':comp_post_code', "%$comp_post_code%", PDO::PARAM_STR);
$STH->execute();
$STH->setFetchMode(PDO::FETCH_ASSOC);
return $STH;
}

我有 cat1 到 cat10 .. 我想在不写长代码的情况下获得数据输出

 and comp_post_code like :comp_post_code and cat2 like :cat
 and comp_post_code like :comp_post_code and cat3 like :cat

我有办法解决这个问题......我们可以编写 ong 代码并退出,但仍然想知道我们可以用其他方式吗?

表结构

comp_post_code 猫1 猫2 三类 第四类

【问题讨论】:

  • 可以发一下表结构吗? (cat1..cat10 是表列?)
  • @FlorinelChis 是的,你是,我已经编辑了问题...
  • :cat 应该在 all cat[1-n] 或 any 列中找到?
  • @FlorinelChis 我正在搜索,如果邮政编码像 B91% 并且猫在任何猫中(从猫 1 到猫 9)它应该在输出中......

标签: php mysql pdo


【解决方案1】:

根据您的 cmets 提供的详细信息:

SELECT * from uk_data where 
comp_post_code like :comp_post_code AND (
cat1 like :cat OR
cat2 like :cat OR
cat3 like :cat OR
cat4 like :cat OR
cat5 like :cat OR
cat6 like :cat OR
cat7 like :cat OR
cat8 like :cat OR
cat9 like :cat OR
cat10 like :cat
)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-06-21
    • 2012-02-09
    • 1970-01-01
    • 2014-02-24
    • 1970-01-01
    • 2017-03-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多