简体   繁体   English

帮助我如何使用PHP和mySQL进行高效编码

[英]Help on how can I code this efficiently and effectively using PHP and mySQL

In my mySQL database under products table, there are a lot of shops selling different kind of things, and every product has it's category name as category and category id as category_id . 在我的mySQL数据库中的products表下,有许多商店出售不同种类的商品,每个产品都有其类别名称作为category和类别id作为category_id Eventually this table will be huge sometime and I will update it quite often from the given XMLs of the shops. 最终,该表有时会很大,我将根据商店的给定XML对其进行很多更新。

Some of the products of the same shop, have the same category and category_id , this is logical. 同一家商店的某些产品具有相同的categorycategory_id ,这是合乎逻辑的。 A second shop that sells the same things may have a quite same category name easy to manually match it with a smilar one and 99% different category_id . 出售相同商品的第二家商店可能具有完全相同的category名称,可以很容易地手动将其与一个带有相似符号的和99%不同的category_id匹配。

In the website the user will see some categories let's say Bicycles for example. 用户在网站上将看到一些类别,例如“自行车”。 When he clicks on that category, there will be some subcategories like Bicycle, Helmets, Gloves, Accessories. 当他单击该类别时,将出现一些子类别,如自行车,头盔,手套,配件。

And this is my problem/question. 这是我的问题。 Let's take Helmets. 让我们戴头盔。

Some shops may have listed the Helmets category as Helmets, Head Accessories, Head Gear and so on. 一些商店可能将“头盔”类别列出为“头盔”,“头部配件”,“头部装备”等。 So if I collect all of these, I will have a list of category or a list of category_id that point to the same thing, Helmets. 因此,如果我收集了所有这些信息,我将获得一个category列表或一个指向同一事物(头盔)的category_id列表。 As I said, there may be more than one shops that sells the same product and there categories names are quite the same. 正如我所说,可能有不止一家商店销售相同的产品,并且类别名称完全相同。

What I thought is to find this list manually and create a SQL query like below for every subcategory so it will display the products to the user. 我的想法是手动找到此列表,并为每个子类别创建一个如下所示的SQL查询,以便它将产品显示给用户。 (pseudocode) (伪代码)

SELECT * FROM products WHERE category_id = 1312 or 453 or 54332 or 6734 or or or

What I don't know is if this will be slow in a table that will sometime have more than 300.000 products. 我不知道这是否会在某个表中有时会超过300.000个产品的情况下变慢。

Another approach that I thought is to find all the "same" categories and change their category_id so they will all have the same id (if this will help dramatically the speed and of course if the above query is really slow). 我认为的另一种方法是找到所有“相同”类别并更改它们的category_id以便它们都具有相同的ID(如果这将极大地提高速度,当然,如果上述查询的速度很慢)。

SELECT * FROM products WHERE category_id = 1312 ONLY ONE CATEGORY ID

This approach will lead me to an issue that, because I will update the table often from the updated XMLs provided by the shops I will have to rename again all the "same" category_id to the predefined category_id . 这种方法将导致一个问题,因为我将经常从商店提供的更新的XML中更新表,因此我将不得不再次将所有“相同”的category_id重命名为预定义的category_id I guess this is not a big issue, it is just a small PHP code on my xml to mysql script. 我想这不是一个大问题,这只是我的xml到mysql脚本上的一小段PHP代码。

Please share me your thoughts if my thinking is correct, and give me your thoughts on how to code this thing. 如果我的想法是正确的,请分享我的想法,并告诉我关于如何编写代码的想法。 Any info is appreciated. 任何信息表示赞赏。

Thank you. 谢谢。

A shortcut for multiple OR is IN 多个OR的快捷方式是IN

SELECT * FROM products WHERE category_id IN (1312,453,54332,6734)

This will make it easier for you to create queries, since you can do 因为您可以做,这将使您更轻松地创建查询

$listOfIDs = implode(',',array(1312,453,54332,6734));
$query = "SELECT * FROM products WHERE category_id IN ($listOfIDs)";

and it will work equally well if there's only one ID or several IDs in the array. 如果阵列中只有一个或多个ID,它将同样有效。

No, having to look up multiple IDs will not slow down your query. 不,必须查找多个ID不会降低查询速度。 You will want however to have an index on category_id column. 但是,您将希望在category_id列上有一个索引。

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

相关问题 如何将这个有效的mysql语句有效地转换为PDO? - How can I convert this working mysql statement to PDO effectively? 如何借助PHP在MySQL数据库中执行搜索操作? - How can I perform search operation in MySQL database with help of PHP? 我应该如何重构我的代码(PHP和MySQL)以更有效地使用主/从数据库配置? - How should I refactor my code (PHP & MySQL) to work more efficiently with a Master / slave database configuration? 如何在Laravel中有效地构造此代码? - How can I efficiently structure this code in Laravel? 如何使用 PHP 更高效地将 csv 文件导入 MySQL 数据库? - How can I import csv file to MySQL database more efficiently with PHP? 如何使用PHP有效地构建站点地图? - How to effectively build sitemap using PHP? 如何修复我的代码以使用php,mysql进行调查将3个值保存在数组中? - How can I fix my code to save 3 values in array using php, mysql to make a survey? 如何使用MySQL从MySQL表中检索数据并将其插入QR码? - How can I retrieve data from MySQL table and insert it into QR code using PHP? 我如何使用此 html 代码在 php 中的 mysql localhost 中连接和登录 - how i can connect and login in mysql localhost in php by using this html code PHP mysql代码有助于解码 - PHP mysql code help decoding
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM