简体   繁体   中英

MySQL query to find out specific region data from all strings in database table and list all related banners

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.

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

As mentioned in my comment, if you have any control over t_banner, it would make sense to turn this into a linking table that contains the foreign keys for the regions. You could create the table like this:

CREATE TABLE `t_banner` (
`bannerpkid` integer,
`regionfkid` integer
);

Then you could populate the banner and region keys with something like this:

INSERT INTO `t_banner` (`bannerpkid`, `regionfkid`) VALUES
(1, 8),
(1, 9),
(1, 10),
(2, 2),
(2, 4),
(2, 6),
(3, 5),
(3, 7),
(3, 9),
(4, 3), 
(4, 6),
(4, 9),
(5, 1),
(5, 2),
(5, 3),
(6, 5),
(6, 8),
(6, 10)
;

From there, you could write a query like this that would give you all banners for a given region:

SELECT DISTINCT
    bannerpkid
FROM
    t_banner tb
      JOIN
    tr_regions tr on tr.regionpkid = tb.regionfkid
WHERE
    regionname = 'Northwest Alabama'
ORDER BY
    bannerpkid
;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM