如何检查某些字符串是否是来自两个单独列的值的组合,即表 PHP MySql
How to check if some string is combination of values from two separate columns i.e tables PHP MySql
我需要最便宜的方法来检查我的 url slug 是否由来自两个单独 table 的两个单独列的值形成。
我将使用带有商店和位置的虚拟示例,以使其更易于阅读。
我有以下 url:
www.domain.com/store-location
这可能是,例如:
www.domain.com/three-words-store-chicago or
www.domain.com/nicestore-new-york-or-some-neighbourhood-with-more-words or
www.domain.com/oneword-oneword
(你懂的)
商店位于 table 个称为商店的位置,位于 table 个称为位置的位置。
所有的组合在理论上都是可能的。
因此,我需要一些巧妙的 mysql 查询与 php 相结合,这将检查我的 slug(.com/ 之后的内容)是否是商店+位置的确切组合。因此,为了使其更具描述性:
url: www.domain.com/cool-store-los-angeles
检查 table stores.slug_stores 中是否有 "cool-store",table locations.slug_location 中是否有 "los-angeles"。正如您在上面看到的,两者的字数都是未定义的,所以我没有任何可能的分隔符。
它一定是最便宜的方法,因为 table 和 table 都有大约 1000 行。请帮助并谢谢你们!
ps。重要提示:我不得以任何方式更改网址
编辑:这是真实的项目,网站。取决于 url 即 slug 我 return 一些带有数据的视图。因此,我需要检查 www.domain.com/nicestore-nicecity Nicestore 和 Nicecity 是否存在于 table 的商店和位置中,如果不存在,或者是否存在其他类似 www.domain 的东西。 com/nicestore-nicecityBLABLA 用 404 杀死那个页面。否则,如果有 Nicestore 和 Nicecity 到 return 某个页面填充了相关数据。到目前为止,我尝试用 "nicestore-nicecity" 等形成的 slug 单独制作 table 并将其用于查询“SELECT whatever FROM slugs WHERE whatever = 'nicestore-nicecity' 并且如果有行 return 无论我需要显示页面...简化...但是,这个单独的 table 很难维护。如果 nicestore 移动到 uglycity,或者如果它更改名称,或者如果您添加一个新的商店或新城市。我希望我现在更清楚了 ;-)
我认为您可以在 mysql 中进行如下查询,如果之后请在 php 中进行查询。从你的描述来看,这些表之间似乎没有任何可能的连接,所以我认为需要并集。
select col1,col2 from stores where slug_stores = ?
union
select col1,col2 from locations where slug_location = ?
这是我对您的系统的了解...
- 您有一个
stores
table 列 slug_stores
- 您有一个
locations
table 列 slug_location
我假设每个 table 都有一个某种类型的 id
列。我还假设他们有一个 多对多 关系,使用第三个连接点 table,类似
CREATE TABLE store_locations (
store_id <type>,
location_id <type>,
PRIMARY KEY (store_id, location_id),
FOREIGN KEY (store_id) REFERENCES stores(id),
FOREIGN KEY (location_id) REFERENCES locations(id)
);
如果你没有定义这种关系,我真的不知道你是如何维护你的商店位置的。
我建议创建一个 VIEW
来计算和表示您的 URL。例如...
CREATE VIEW store_location_urls AS
SELECT
sl.store_id,
sl.location_id,
CONCAT_WS('-', s.slug_stores, l.slug_location) AS slug
FROM store_locations sl
INNER JOIN stores s ON sl.store_id = s.id
INNER JOIN locations l ON sl.location_id = l.id;
现在您可以使用此视图执行以下操作...
检查请求 URL slug 是否有效
SELECT store_id, location_id FROM store_location_urls WHERE slug = ?
如果此 returns 一条记录,您可以进一步查询 stores
和 locations
table 以获取呈现页面所需的任何额外数据(或甚至只是将它们加入原始查询中)。否则,使用
http_response_code(404);
获取特定商店的所有 URL 子弹
SELECT slug FROM store_location_urls WHERE store_id = ?
同样,您可以获得特定位置的所有 URL 鼻涕虫
额外注意...由于连接字符串,您在 stores.slug_stores
和 locations.slug_location
上的任何索引对于上面的 VIEW
都是无用的。另一种方法是使用真正派生的 table(就像您目前拥有的)并使用触发器维护它。
我假设您没有任何 id
值可以用来 JOIN
您的 table,并且您没有能力创建这样的值值。在这种情况下,由于您的 store/location 组合可能与 oneword-oneword
一样短,因此 slug 的第一个和最后一个词与您可以搜索的一样多。您可以使用 SUBSTRING_INDEX
提取 slug 的开始和结束部分,并使用它来缩小每个 table before 中的匹配集,然后尝试比较整个细绳。在我的示例中,我使用 SQL 变量来存储 slug:
SET @store = 'cool-store-los-angeles'
SELECT *
FROM (SELECT *
FROM stores
WHERE store LIKE CONCAT(SUBSTRING_INDEX(@store, '-', 1), '%')) s
JOIN (SELECT *
FROM locations
WHERE location LIKE CONCAT('%', SUBSTRING_INDEX(@store, '-', -1))) l
WHERE CONCAT(s.store, '-', l.location) = @store
这将 return 所有与 cool-store-los-angeles
关联的数据,假设存在这样的商店。
我需要最便宜的方法来检查我的 url slug 是否由来自两个单独 table 的两个单独列的值形成。
我将使用带有商店和位置的虚拟示例,以使其更易于阅读。 我有以下 url:
www.domain.com/store-location
这可能是,例如:
www.domain.com/three-words-store-chicago or
www.domain.com/nicestore-new-york-or-some-neighbourhood-with-more-words or
www.domain.com/oneword-oneword
(你懂的)
商店位于 table 个称为商店的位置,位于 table 个称为位置的位置。 所有的组合在理论上都是可能的。 因此,我需要一些巧妙的 mysql 查询与 php 相结合,这将检查我的 slug(.com/ 之后的内容)是否是商店+位置的确切组合。因此,为了使其更具描述性:
url: www.domain.com/cool-store-los-angeles
检查 table stores.slug_stores 中是否有 "cool-store",table locations.slug_location 中是否有 "los-angeles"。正如您在上面看到的,两者的字数都是未定义的,所以我没有任何可能的分隔符。
它一定是最便宜的方法,因为 table 和 table 都有大约 1000 行。请帮助并谢谢你们!
ps。重要提示:我不得以任何方式更改网址
编辑:这是真实的项目,网站。取决于 url 即 slug 我 return 一些带有数据的视图。因此,我需要检查 www.domain.com/nicestore-nicecity Nicestore 和 Nicecity 是否存在于 table 的商店和位置中,如果不存在,或者是否存在其他类似 www.domain 的东西。 com/nicestore-nicecityBLABLA 用 404 杀死那个页面。否则,如果有 Nicestore 和 Nicecity 到 return 某个页面填充了相关数据。到目前为止,我尝试用 "nicestore-nicecity" 等形成的 slug 单独制作 table 并将其用于查询“SELECT whatever FROM slugs WHERE whatever = 'nicestore-nicecity' 并且如果有行 return 无论我需要显示页面...简化...但是,这个单独的 table 很难维护。如果 nicestore 移动到 uglycity,或者如果它更改名称,或者如果您添加一个新的商店或新城市。我希望我现在更清楚了 ;-)
我认为您可以在 mysql 中进行如下查询,如果之后请在 php 中进行查询。从你的描述来看,这些表之间似乎没有任何可能的连接,所以我认为需要并集。
select col1,col2 from stores where slug_stores = ?
union
select col1,col2 from locations where slug_location = ?
这是我对您的系统的了解...
- 您有一个
stores
table 列slug_stores
- 您有一个
locations
table 列slug_location
我假设每个 table 都有一个某种类型的 id
列。我还假设他们有一个 多对多 关系,使用第三个连接点 table,类似
CREATE TABLE store_locations (
store_id <type>,
location_id <type>,
PRIMARY KEY (store_id, location_id),
FOREIGN KEY (store_id) REFERENCES stores(id),
FOREIGN KEY (location_id) REFERENCES locations(id)
);
如果你没有定义这种关系,我真的不知道你是如何维护你的商店位置的。
我建议创建一个 VIEW
来计算和表示您的 URL。例如...
CREATE VIEW store_location_urls AS
SELECT
sl.store_id,
sl.location_id,
CONCAT_WS('-', s.slug_stores, l.slug_location) AS slug
FROM store_locations sl
INNER JOIN stores s ON sl.store_id = s.id
INNER JOIN locations l ON sl.location_id = l.id;
现在您可以使用此视图执行以下操作...
检查请求 URL slug 是否有效
SELECT store_id, location_id FROM store_location_urls WHERE slug = ?
如果此 returns 一条记录,您可以进一步查询
stores
和locations
table 以获取呈现页面所需的任何额外数据(或甚至只是将它们加入原始查询中)。否则,使用http_response_code(404);
获取特定商店的所有 URL 子弹
SELECT slug FROM store_location_urls WHERE store_id = ?
同样,您可以获得特定位置的所有 URL 鼻涕虫
额外注意...由于连接字符串,您在 stores.slug_stores
和 locations.slug_location
上的任何索引对于上面的 VIEW
都是无用的。另一种方法是使用真正派生的 table(就像您目前拥有的)并使用触发器维护它。
我假设您没有任何 id
值可以用来 JOIN
您的 table,并且您没有能力创建这样的值值。在这种情况下,由于您的 store/location 组合可能与 oneword-oneword
一样短,因此 slug 的第一个和最后一个词与您可以搜索的一样多。您可以使用 SUBSTRING_INDEX
提取 slug 的开始和结束部分,并使用它来缩小每个 table before 中的匹配集,然后尝试比较整个细绳。在我的示例中,我使用 SQL 变量来存储 slug:
SET @store = 'cool-store-los-angeles'
SELECT *
FROM (SELECT *
FROM stores
WHERE store LIKE CONCAT(SUBSTRING_INDEX(@store, '-', 1), '%')) s
JOIN (SELECT *
FROM locations
WHERE location LIKE CONCAT('%', SUBSTRING_INDEX(@store, '-', -1))) l
WHERE CONCAT(s.store, '-', l.location) = @store
这将 return 所有与 cool-store-los-angeles
关联的数据,假设存在这样的商店。