简体   繁体   English

MySQL - 包含外键 ID 和外键值的搜索表

[英]MySQL - Search table that contains Foreign key id with foreign key value

I'm making search engine inside my platform, and I want to search specific table called business_services , that table contains a foreign key from another table called services .我正在我的平台内制作搜索引擎,我想搜索名为business_services的特定表,该表包含来自另一个名为services的表的外键。

The ideas that every business provide some of these services but I only store service ids in business_services table.每个企业都提供其中一些服务的想法,但我只将服务 ID 存储在business_services表中。

how I can search business_services table for a service using only it's id while I'm searching with name如何在使用名称搜索时仅使用它的 id 来搜索business_services表中的服务

and if that's not possible, I can make another column inside business_services table to store service name but how I can update that name whenever service name changes如果这不可能,我可以在business_services表中创建另一列来存储服务名称,但是我如何在服务名称更改时更新该名称

business商业

| id  | name_en | name_fr | status |
|-----|---------|---------|--------|
| 134 | name 1  | nom 1   | 1      |
| 432 | name 2  | nom 2   | 1      |
| 325 | name 3  | nom 3   | 2      |

services服务

| id | name_en   | name_fr   | status |
|----|-----------|-----------|--------|
| 5  | service 1 | service 1 | 1      |
| 9  | service 2 | service 2 | 1      |
| 4  | service 3 | service 3 | 1      |

business_services商业服务

| id | business_id | service_id | status |
|----|-------------|------------|--------|
| 1  | 134         | 5          | 1      |
| 2  | 432         | 9          | 1      |
| 3  | 325         | 4          | 1      |

If you are looking for example for service fr name called service 1.如果您正在寻找名为 service 1 的服务 fr 名称的示例。

select
    bs.*, -- all columns from business_services
    
    -- columns from services table
    s.name_en,
    s.name_fr,
    s.status
from
    business_services bs inner join services s on bs.service_id=s.id
where 
    s.name_fr = 'service 1';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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