doudeng3008 2015-05-07 08:45
浏览 62

mysql concat和de-concat

I don't know well how to explain my problem...

This is my sql request :

SELECT DISTINCT CONCAT (nompropre, ' ', Auteur, ' de ', localite) AS auteur FROM Actes ORDER BY nompropre

It's results are passed on to a select in a form, showing things like this :

<option value=" seigneur de Witon"> seigneur de Witon</option>
<option value=" sénéchal de Hainaut"> sénéchal de Hainaut</option>
<option value=" voir-jurés de Tournai"> voir-jurés de Tournai</option>
<option value="Adalbéron évêque de Verdun">Adalbéron évêque de Verdun</option>
<option value="Adam évêque de Morinie">Adam évêque de Morinie</option>

The problem is that when that form is sent, it should result in the following sql query, where the last line of the above options is selected :

SELECT * FROM Actes, Bibliographie WHERE id = idBiblio AND nompropre = 'Adam' and Auteur = 'évêque' AND localite = 'Morinie';

I would need to de-concat the select options, knowing that both nompropre and/or localite could be empty strings...

Any clues... ?

  • 写回答

1条回答 默认 最新

  • dqdl6469 2015-05-07 08:52
    关注

    Explode should do a task:

    $str = ' seigneur de Witon';
    $parts = explode (' ', $str);
    
    echo '<pre>';
    print_r($parts);
    
    /*
        returns:
        Array
        (
            [0] => 
            [1] => seigneur
            [2] => de
            [3] => Witon
        )
    
        OR if the name is given:
    
        Array
        (
            [0] => Peter
            [1] => seigneur
            [2] => de
            [3] => Witon
        )
    */
    

    SQL query will be:

    $sql = "SELECT * FROM Actes, Bibliographie WHERE id = idBiblio " . (!empty($parts[0]) ? "AND nompropre = '" . $parts[0] . "'" : '') . " and Auteur = '" . $parts[1] . "' AND localite = '" . $parts[3] . "'";
    

    PS: don't forget to sanitize data, use mysqli_real_escape_string function for the strings.

    评论

报告相同问题?

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥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系统搭建请教(跨境电商用途)