Count search criteria record based on search done by user (MYSQL PHP)

偶尔善良 提交于 2019-11-30 23:53:58

I've tried to do this before, and it can get very slow depending on how many filters you allow and how many hotels you list, not to mention how you deal with duplicate hotels.

Ultimately though you will have very few filter options

  • Property type : normalise this in a separate table
  • Bedrooms : store this as a tinyint or smallint (either unsigned), can't imagine there being properties above 255 bedrooms, and definitely not above 65k
  • Location : normalise this in a separate table, ideally in a tree format to ensure relationships are noted
  • Star rating : this can be stored as a tinyint unsigned

Now your problem here is that if someone applies a filter for 3 bedrooms upwards, you still should be getting values for 2 bedrooms, 1 bedroom, as changing the filter back to that will yield results.

At the end of the day I addressed this using a very large memory table, some logic to build WHERE and JOIN statements, and an individual query counting up records within a set grouping. This was for doing similar to users holiday search results though, and as such the data was considered entirely transient. For your purposes a far smaller memory table is likely to be acceptable, however the principle is similar.

msgmash.com

Use the GROUP BY clause in conjunction with COUNT on your SELECT statement. For example, I defined a little test table such as

create table rooms (
rooms INT NOT NULL,
name VARCHAR(10)
)

And then ran the query

SELECT rooms,COUNT(*) FROM rooms GROUP BY rooms;

This gives you a result with each room count and the number of entries with that value.

What you're trying to achieve is called faceted search.

This isn't a problem suited to a relational database like MySQL.

You can use ElasticSearch or AWS CloudSearch and their facet features.

Just remember that these are search servers and don't replace your main database. They only perform search operations that return the IDs that correspond to the actual records stored in your database. You still need MySQL (or MongoDB etc).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!