doumingchen3628 2015-03-14 01:26
浏览 66
已采纳

从表中选择缺少某些WHERE条件的表

I have following sql request

        if(isset($_GET['room'])) {

            if($_GET['status'] == 'all' && $_GET['room'] == 'all'){
                $stmt = $pdo->prepare("SELECT DISTINCT flatnetto FROM flat ORDER BY flatnetto");
                $stmt->execute(array($_GET['room'], $_GET['status']));
            }else if($_GET['status'] == 'all'){
                $stmt = $pdo->prepare("SELECT DISTINCT flatnetto FROM flat WHERE kk = ? ORDER BY flatnetto");
                $stmt->execute(array($_GET['room']));
            }else if($_GET['room'] == 'all'){
                $stmt = $pdo->prepare("SELECT DISTINCT flatnetto FROM flat WHERE sold = ? ORDER BY flatnetto");
                $stmt->execute(array($_GET['status']));
            }else{
                $stmt = $pdo->prepare("SELECT DISTINCT flatnetto FROM flat WHERE kk = ? AND sold = ? ORDER BY flatnetto");
                $stmt->execute(array($_GET['room'], $_GET['status']));
            }               

            $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
        }

It is possible to make just one sql request from code above? I mean, it is possible to make just one request

    $stmt = $pdo->prepare("SELECT DISTINCT flatnetto FROM flat WHERE kk = ? AND sold = ? ORDER BY flatnetto");

but condition "kk" and "sold" may or may not exist (will have unsortable "all" value). So for example if "kk" not exist just ignore it and continue selecting only with condition "sold".

Code above is working but it is pretty annoying to define all possible options.


Status can have values (Sold, Reserved, All)

Room can have values (1kk, 2kk, 3kk, All)

Value "all" means that for example I am interesting on 2kk room and no matter if is "sold" or "reserved" just show me all of them.

UPDATED Question.

As is wrote on answer below it is possible to make one sql request. But the problem persist because we just move "possible option definition" to sql query but I still have to define all possible option. I can not imagine how to define all possible options if we will have not just two input but for example "room, status ,price1, price2, floor, size ..." and all of them can have own value but also value "all". In this case we have to define all possible options from all possible variations (in this case more than 20 possible variations). I think this is not way how it work in bigger projects.

  • 写回答

2条回答 默认 最新

  • duangeli1334 2015-03-14 08:56
    关注

    You can use the following code to generate your SQL query

    $param=Array();
    
    ## associate query parameters to sql column
    $keys=Array("room" => "kk", "status" => "sold");
    
    ## generate parameter list
    foreach ($keys as $k=>$v)
    if (isset($_GET[$k])&&($_GET[$k]!='all'))
       $param[$v]=$_GET[$k];
    
    ## generate query
    $query="SELECT DISTINCT flatnetto FROM flat WHERE ".
        implode(" = ? AND ", array_keys($param)).
        (count($param) ? " = ?" : "1")
        ." ORDER BY flatnetto";
    
    ## execute
    $stmt = $pdo->prepare($query);
    $stmt->execute($param);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。