PHP/MySQL 关系数据库模式的搜索代码和逻辑

发布于 2024-11-16 20:45:15 字数 847 浏览 0 评论 0原文

我创建了这个数据库模式,并在此处的几个用户的帮助下,我有一个数据库,该数据库将用户提交的业务条目存储在业务表中,这些条目另外分组在类别表中的大约 10 个类别中的一个或多个类别下,在tbl_works_catagories 表通过将bus_id 与类别id 进行匹配。

例如,bus_id 21 可以与 catagory_id 1、2、5、7、8 相关联。

CREATE TABLE `business` (
`bus_id` INT NOT NULL AUTO_INCREMENT, 
`bus_name` VARCHAR(50) NOT NULL, 
`bus_dscpn` TEXT NOT NULL, 
`bus_url` VARCHAR(255) NOT NULL,
PRIMARY KEY (`bus_id`)
)

CREATE TABLE `categories` (
`category_id` INT NOT NULL AUTO_INCREMENT, 
`category_name` VARCHAR(20) NOT NULL, 
PRIMARY KEY (`category_id`)
)

CREATE TABLE `tbl_works_categories` (
`bus_id` INT NOT NULL, 
`category_id` INT NOT NULL
)

现在,我接下来要做的是一个搜索功能,它将根据类别返回企业。例如,假设进入业务表的企业之一是面包师,当它被输入时,它被分类在食品 (catagory_id 1) 和外卖 (catagory_id 2) 下。

因此,访问者搜索“食品”类别下列出的企业,就会返回我们友好的邻里面包师。

与所有 PHP/MySQL 一样,我只是无法(无论如何一开始)理解逻辑,更不用说代码了!

I have created this database schema and with help from several users on here, I have a database which takes user submitted business entries stored in the business table, which are additionally grouped under one or several of about 10 catagories from the catagories table, in the tbl_works_catagories table by matching the bus_id to the catagory id.

For example, bus_id 21 could be associated with catagory_id 1, 2, 5, 7, 8.

CREATE TABLE `business` (
`bus_id` INT NOT NULL AUTO_INCREMENT, 
`bus_name` VARCHAR(50) NOT NULL, 
`bus_dscpn` TEXT NOT NULL, 
`bus_url` VARCHAR(255) NOT NULL,
PRIMARY KEY (`bus_id`)
)

CREATE TABLE `categories` (
`category_id` INT NOT NULL AUTO_INCREMENT, 
`category_name` VARCHAR(20) NOT NULL, 
PRIMARY KEY (`category_id`)
)

CREATE TABLE `tbl_works_categories` (
`bus_id` INT NOT NULL, 
`category_id` INT NOT NULL
)

Now, what i want to do next is a search function which will return businesses based on the catagory. For example, say one of the businesses entered into the business table is a bakers and when it was entered, it was catagorised under Food (catagory_id 1) and take-away (catagory_id 2).

So a visitor searches for businesses listed under the Food catagory, and is returned our friendly neighbourhood baker.

As with all PHP/MySQL, i just can't (initially anyway) get my head around the logic, never mind the code!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

苦妄 2024-11-23 20:45:15

您应该在表中设置外键以将它们链接在一起。

CREATE TABLE `business` (
`bus_id` INT NOT NULL AUTO_INCREMENT, 
`bus_name` VARCHAR(50) NOT NULL, 
`bus_dscpn` TEXT NOT NULL, 
`bus_url` VARCHAR(255) NOT NULL,
PRIMARY KEY (`bus_id`)
)

CREATE TABLE `categories` (
`category_id` INT NOT NULL AUTO_INCREMENT, 
`category_name` VARCHAR(20) NOT NULL, 
PRIMARY KEY (`category_id`)
)

CREATE TABLE `tbl_works_categories` (
`bus_id` INT NOT NULL, 
`category_id` INT NOT NULL,
FOREIGN KEY (`bus_id`) REFERENCES business(`bus_id`),
FOREIGN KEY (`category_id`) REFERENCES categories(`category_id`)
)

那么您的搜索查询将类似于:

SELECT b.*
FROM business b, categories c, tbl_works_categories t
WHERE 
   b.bus_id = t.bus_id AND
   c.category_id = t.category_id AND
   c.category_id = *SOME SEARCH VALUE*

使用 JOIN 的搜索查询将写为:

SELECT b.*
FROM business b
  JOIN tbl_works_categories t
    ON b.bus_id = t.bus_id
  JOIN categories c
    ON c.category_id = t.category_id
WHERE c.category_id = *SOME SEARCH VALUE*

You should setup foreign keys in your tables to link them together.

CREATE TABLE `business` (
`bus_id` INT NOT NULL AUTO_INCREMENT, 
`bus_name` VARCHAR(50) NOT NULL, 
`bus_dscpn` TEXT NOT NULL, 
`bus_url` VARCHAR(255) NOT NULL,
PRIMARY KEY (`bus_id`)
)

CREATE TABLE `categories` (
`category_id` INT NOT NULL AUTO_INCREMENT, 
`category_name` VARCHAR(20) NOT NULL, 
PRIMARY KEY (`category_id`)
)

CREATE TABLE `tbl_works_categories` (
`bus_id` INT NOT NULL, 
`category_id` INT NOT NULL,
FOREIGN KEY (`bus_id`) REFERENCES business(`bus_id`),
FOREIGN KEY (`category_id`) REFERENCES categories(`category_id`)
)

Then your search query would be something like:

SELECT b.*
FROM business b, categories c, tbl_works_categories t
WHERE 
   b.bus_id = t.bus_id AND
   c.category_id = t.category_id AND
   c.category_id = *SOME SEARCH VALUE*

which using JOIN would be written as:

SELECT b.*
FROM business b
  JOIN tbl_works_categories t
    ON b.bus_id = t.bus_id
  JOIN categories c
    ON c.category_id = t.category_id
WHERE c.category_id = *SOME SEARCH VALUE*
天生の放荡 2024-11-23 20:45:15

也许你想要这样的东西:

SELECT `bus_id` FROM `tbl_works_categories` WHERE `category_id` = *some id from the search* 
        AND `category_id` = *some other id from the search*;

虽然你需要这些 ids - 有几种方法可以做到这一点,我将描述可能是最直接的......

你从 $_POST 获得类别,所以我们假设你有 2 个他们进入了。 (食物和外卖)。按照你想要的方式解析这些,有多种方法,但重点是它们来自 $_POST。

对您找到的每个结果执行此类操作:

SELECT `category_id` FROM `categories` WHERE `category_name` LIKE '%*the name from $_POST*%';

将这些结果存储在一个数组中...根据您拥有的数量,您可以构建一个类似于我首先描述的适用查询。 (请记住,您不需要 and AND ,如果您从此处的第二个查询返回 > 1category_id,则必须检测到这一点)

我不会讨论安全性之类的事情..在执行以下查询时一定要小心包含用户提交的数据。

另一种解决方案可能涉及连接,但不太确定我的脑海中会是什么样子。

祝你好运。

Maybe you want something like this:

SELECT `bus_id` FROM `tbl_works_categories` WHERE `category_id` = *some id from the search* 
        AND `category_id` = *some other id from the search*;

Although you'd need those ids- there are a few ways to do this, I'll describe probably the most straight forward...

You get categories from $_POST, so let's just say you have 2 of them entered. (Food, and take-away). Parse these however you want, there are multiple ways, but the point is they're coming from $_POST.

execute this sort of thing for each one you find:

SELECT `category_id` FROM `categories` WHERE `category_name` LIKE '%*the name from $_POST*%';

Store these results in an array...based on how many you have there you can build an applicable query similar to the one I describe first. (Keep in mind you don't need and AND there, that's something you have to detect if you return > 1 category_id from the second query here)

I'm not going over things like security..always be careful when executing queries that contain user submitted data.

An alternate solution might involve a join, not too sure what that'd look like off the top of my head.

Good luck.

我们只是彼此的过ke 2024-11-23 20:45:15

如果您想要与给定类别 ID 相关的所有业务,您的 SQL 语句将如下所示:

SELECT `business`.`bus_name`
FROM `business`
WHERE `business`.`bus_id` = `tbl_works_categories`.`bus_id`
  AND `categories`.`category_id` = `tbl_works_categories`.`category_id`
  AND `categories`.`category_id` = 1;

其中 1 在本例中是您的食品类别,但也可以是您的 PHP 变量,其中存储用户选择的类别的ID。

还有一个提示:请务必以复数或单数命名您的表。您将两者混合在一起,可能会感到困惑。

If you want all businesses that are related to the given category-id, your SQL-statement would look something like this:

SELECT `business`.`bus_name`
FROM `business`
WHERE `business`.`bus_id` = `tbl_works_categories`.`bus_id`
  AND `categories`.`category_id` = `tbl_works_categories`.`category_id`
  AND `categories`.`category_id` = 1;

Where 1 in this case is your food-category, but could be your PHP variable where the ID of the category the user selected is stored.

And one hint: Be sure to name your tables either in plurar or in singular. You are mixing both and could get confused.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文