douqiang5933 2016-03-04 15:53
浏览 89
已采纳

MySQL查询从数据库表中的所有字符串中查找特定区域数据并列出所有相关横幅

tr_regions table contains region details of USA states.

INSERT INTO tr_regions`(`regionpkid`,`regionname`,`statepkid`,`statename`) VALUES
(1, 'Alabama Gulf Coast', 13, 'Alabama'),
(2, 'Greater Birmingham', 13, 'Alabama'),
(3, 'Black Belt', 13, 'Alabama'),
(4, 'Central Alabama', 13, 'Alabama'),
(5, 'Lower Alabama', 13, 'Alabama'),
(6, 'Mobile Bay', 13, 'Alabama'),
(7, 'North Alabama', 13, 'Alabama'),
(8, 'Northeast Alabama', 13, 'Alabama'),
(9, 'Northwest Alabama', 13, 'Alabama'),
(10, 'South Alabama', 13, 'Alabama');

In t_banner table, each banner has specific regions keywords. It will help me to display banners to visitors on specific region page on website. (Ex: Banner 1 will display when #8, #9, & #10 regions pages will be accessed by visitors).

INSERT INTO `t_banner` (`bannerpkid`, `keyword`) VALUES
(1,'8|Northeast Alabama|9|Northwest Alabama|10|South Alabama|'),
(2,'2|Greater Birmingham|4|Central Alabama|6|Mobile Bay|'),
(3,'5|Lower Alabama|7|North Alabama|9|Northwest Alabama|'),
(4,'3|Black Belt|6|Mobile Bay|9|Northwest Alabama|'),
(5,'1|Alabama Gulf Coast|2|Greater Birmingham|3|Black Belt|'),
(6,'5|Lower Alabama|8|Northeast Alabama|10|South Alabama|');

Now in my control panel, I have select menu where all these regions are listed. Now I want that whenever I select any region then all banners should be displayed which have selected region in their keyword field.

$int_region_pkid=0;
if(isset($_GET["regionpkid"]))
{
    $int_region_pkid=trim($_GET["regionpkid"]);
}

$str_query_select="SELECT * FROM t_banner WHERE ... ";

How can I write above query to get desired result? I tried LOCATE and LIKE functions of MySQL but they are not working here. Someone please help me.

  • 写回答

2条回答 默认 最新

  • dsce23640 2016-03-04 16:02
    关注

    It looks like you need this query (x is your regin). See example at sqlfiddle

    SELECT * FROM t_banner WHERE keyword LIKE 'x%' OR keyword LIKE '%|x|%';
    

    First part 'x%' if keyword starts from desired region and second '%|x|%' if desired region in other part of keyword

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 运动想象脑电信号数据集.vhdr
  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目