简体   繁体   English

在mysql中基于搜索关键字查询特定表

[英]query specific table based on search keyword in mysql

I am creating a search portal in PHP from which user can search for a specific cuisine. 我正在用PHP创建一个搜索门户,用户可以从中搜索特定美食。 In MySQL I have multiple tables for each cuisine and the respective hotel names that offer the cuisine. 在MySQL中,每种美食都有多个表格,以及提供该美食的各自酒店名称。 For example, in table 例如,在表中

How can I query a specific cuisine table based on the cuisine search keyword? 如何根据美食搜索关键字查询特定的美食表?

So if a user enters 'mexican' as the search query, how can it connect to the 'Table2 - Mexican' and return the hotel names from this table? 因此,如果用户输入“墨西哥”作为搜索查询,它如何连接到“表2-墨西哥”并从该表返回酒店名称?

Table1 - Chinese
_______________________
| id   |   hotelname  |
|______|______________|
| 1    |  hotel1      |
| 2    |  hotel2      |
| 3    |  hotel3      |
| 4    |  hotel4      |
| 5    |  hotel5      |
|______|______________|

Table2 - Mexican
_______________________
| id   |   hotelname  |
|______|______________|
| 1    |  hotel1      |
| 2    |  hotel2      |
| 3    |  hotel3      |
| 4    |  hotel4      |
| 5    |  hotel5      |
|______|______________|

Table3 - Pizza
_______________________
| id   |   hotelname  |
|______|______________|
| 1    |  hotel1      |
| 2    |  hotel2      |
| 3    |  hotel3      |
| 4    |  hotel4      |
| 5    |  hotel5      |
|______|______________|

Your database concept is very unflexible. 您的数据库概念非常不灵活。 I think you should put the cuisines into your database as information (ie table content) instead of metadata describing single tables. 我认为您应该将美食作为信息(即表内容)放入数据库中,而不是描述单个表的元数据。 Tables should generally considered to be static just like the code you write to access the database and its tables. 表通常应视为静态的,就像您为访问数据库及其表编写的代码一样。 If you implement the cuisines as different tables you would have to hardwire every cuisine into your code. 如果将菜式实施为不同的表,则必须将每种菜式硬编码到代码中。

Here is a suggestion for a better approach: 这是一种更好方法的建议:

  • Create a hotels table to store all the hotels, 创建一个hotels表来存储所有酒店,
  • Create a cuisines table to store all the different types of cuisines, 创建一个cuisines表来存储所有不同类型的美食,
  • Make an additional table to establish the n:m relationship between the hotel and the cuisine. 新建一个表格,以建立酒店与美食之间的n:m关系。

Example: 例:

hotels:  id, name, address, city, telno, email
cuisine: id, name, description
rel:     cuisine, hotel (where both are the foreign keys to the
                         id columns of the respective tables above)

See also: 也可以看看:

you can check SQL UNION . 您可以检查SQL UNION But instead of having multiple tables with the same fields, you can try normalization to minimize the redundancy and to make queries easier. 但是,您可以尝试使用规范化来最大程度地减少冗余并简化查询,而不是使用具有相同字段的多个表。

Something like: 就像是:

 Hotel Table
 -----------------------------
 id | hotelname    | categoryID
 ------------------------------
 1  | hotel name 1 | 1
 2  | hotel name 2 | 2
 -----------------------------

 Category Table
 -------------------
  id | categoryname 
 -------------------
   1 | chinese
   2 | mexican
 ------------------

And query as simple as: 和查询一样简单:

SELECT a.hotelname, b,categoryname 
      FROM hotel_table a 
      LEFT JOIN category_table b 
      ON a.categoryID = b.id AND b.categoryname LIKE '%mexican%'; 

You might want to check this question to create a many-to-many relationship: 您可能需要检查以下问题以创建多对多关系:
many-to-many and many-to-many intersections 多对多和多对多交叉点

I guess what you would like to achieve is something like this: 我想您想要实现的是这样的:

Table1 - Hotel
_______________________
| id   |   hotelname  |
|______|______________|
| 1    |  hotel1      |
| 2    |  hotel2      |
| 3    |  hotel3      |
| 4    |  hotel4      |
| 5    |  hotel5      |
|______|______________|

Table2 - Cuisine
____________________________________________
| id   | cuisine_name | keywords           |
|______|______________|____________________|
| 1    |  Chinese     | Shandong,Noodles,. |
| 2    |  Mexican     | Tacos,Beans,...    |
| 3    |  Itarian     | Pizza,Pasta,..     |
|______|______________|____________________|

Table3 - HotelCuisine
___________________________________
| id   |  hotel_id  |  cuisine_id |
|______|____________|______________
| 1    |    1       |      2      |
| 2    |    1       |      3      |
| 3    |    2       |      1      |
| 4    |    2       |      2      |
| 5    |    3       |      3      |
|______|____________|_____________|

SQL: SQL:

 SELECT hotelname, cuisine_name FROM Hotel
 INNER JOIN HotelCuisine ON Hotel.id = HotelCuisine.hotel_id
 INNER JOIN Cuisine ON Cuisine.id = HotelCuisine.cuisine_id
 WHERE keywords like '%pizza%'

Result: 结果:

________________________________________
| hotelname     | cuisine_name         |
|_______________|______________________|
| hotel1        | Itarian              |                     
| hotel3        | Itarian              |                     
|_______________|______________________|

DEMO: http://sqlfiddle.com/#!2/961de/1 演示: http ://sqlfiddle.com/#!2/ 961de/1

Hope this helps 希望这可以帮助

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

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