dongshengli6384 2018-08-17 13:40
浏览 30
已采纳

搜索任何表字段中是否存在变量并获取表字段名称

Using DOM on a webpage, I create a variable like this:

$adres = "Bruinbergstraat 2,Bruinberg,8730 Oedelem";

In a MySql table I have a lot of records; each record has at least 1 address field, other records have more address fields (max. 7).

When I use this code to find if the variable exists in any of the address fields:

$command = "Select * from alleclubs where (adres1 like '%" . $adres ."%' or adres2 LIKE '%" . $adres ."%' or adres3 LIKE '%" . $adres ."%' or adres4 LIKE '%" . $adres ."%' or adres5 LIKE '%" . $adres ."%' or adres6 LIKE '%" . $adres ."%' or adres7 LIKE '%" . $adres ."%')";

the result is 0 records found.

How can I solve this so I can get the table field name of the column the variable is in?

  • 写回答

1条回答 默认 最新

  • doudu2591 2018-08-17 14:36
    关注

    To get the table field name of the column the variable is matched with, you can do something like below-

    "select *
           from (select 
                    concat_ws(',',
                        if( adres1 LIKE '%$adres%','adres1',NULL),
                        if( adres2 LIKE '%$adres%','adres2',NULL),
                        if( adres3 LIKE '%$adres%','adres3',NULL),
                        if( adres4 LIKE '%$adres%','adres4',NULL),
                        if( adres5 LIKE '%$adres%','adres5',NULL),
                        if( adres6 LIKE '%$adres%','adres6',NULL),
                        if( adres7 LIKE '%$adres%','adres7',NULL)
                    ) as 'matched_column_names',stamnummer
                from alleclubs ) derived_table
    where matched_column_names != '' "
    

    Then, when iterating each row through PHP, you can just use explode() over , to get each column name.

    Note: You can also use CASE in MySQL but CASE stops when there is a match. The above query will give you all column names that matched with $adres for each single row.

    UPDATE:

    You can directly check this in your phpMyAdmin with your sample Bruinbergstraat 2,Bruinberg,8730 Oedelem as below-

    select *
           from (select 
                    concat_ws(',',
                        if( adres1 LIKE '%Bruinbergstraat 2,Bruinberg,8730 Oedelem%','adres1',NULL),
                        if( adres2 LIKE '%Bruinbergstraat 2,Bruinberg,8730 Oedelem%','adres2',NULL),
                        if( adres3 LIKE '%Bruinbergstraat 2,Bruinberg,8730 Oedelem%','adres3',NULL),
                        if( adres4 LIKE '%Bruinbergstraat 2,Bruinberg,8730 Oedelem%','adres4',NULL),
                        if( adres5 LIKE '%Bruinbergstraat 2,Bruinberg,8730 Oedelem%','adres5',NULL),
                        if( adres6 LIKE '%Bruinbergstraat 2,Bruinberg,8730 Oedelem%','adres6',NULL),
                        if( adres7 LIKE '%Bruinbergstraat 2,Bruinberg,8730 Oedelem%','adres7',NULL)
                    ) as 'matched_column_names',stamnummer
                from alleclubs ) derived_table
    where matched_column_names != '';
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100