点击一个分类,把当前分类及其所有子分类的所有产品列出来
上面是分类表,产品表就是 关联 分类表的 cate_id ,确定产品所属的分类
现在我想要的效果是 点击一个分类,把当前分类及其所有子分类的所有产品列出来
请大神指教
想的头都破了,递归?还是?怎么弄呢?
回复讨论(解决方案)
点击了分类,于是就知道了分类号
查询数据库,凡是父类号为这个分类号的就是他的子分类了
点击了分类,于是就知道了分类号
查询数据库,凡是父类号为这个分类号的就是他的子分类了
我不是要子分类 我是要 所有子分类下面的产品
奇怪了,有了子分类号,还愁子分类下面的产品吗?
不知道你数据量大不大
小企业站的话,得到父分类ID后,查到所有子分类ID,然后合并到一起:2,5,6,7,8这种
再到产品表sql语句用in关键字查出来,怎么用,搜一下吧...
不清楚in能不能用到索引?
如果数据量比较大,就用冗余换效率
给分类表加一个floor字段,父分类就写0,1级子分类就写1,2级子分类写2。。。
假设你有3层分类,那么在产品表加3个字段,cate,cate1,cate2
产品属于哪个子分类,就把从父到2级子 3个字段都填满
筛选时根据传入的cateid,先查出这个分类的floor,然后查出他的所有上级分类id
剩下就是把所有得到的id作为条件筛选了
这里有个关键,要建一个BTREE索引,以cate到cateN的顺序都包括进来
同数据量方法2要快很多,但他的缺点是子分类移动时还要改产品表
奇怪了,有了子分类号,还愁子分类下面的产品吗?
这里还涉及到子分类的子分类 是所有后代分类,所以我最先想到的就是 递归
如果单单是一层子分类当然好办了,可是无限极分类,子分类层数个数都未知
我想到了一个方案了
不知道你数据量大不大
小企业站的话,得到父分类ID后,查到所有子分类ID,然后合并到一起:2,5,6,7,8这种
再到产品表sql语句用in关键字查出来,怎么用,搜一下吧...
不清楚in能不能用到索引?
如果数据量比较大,就用冗余换效率
给分类表加一个floor字段,父分类就写0,1级子分类就写1,2级子分类写2。。。
假设你有3层分类,那么在产品表加3个字段,cate,cate1,cate2
产品属于哪个子分类,就把从父到2级子 3个字段都填满
筛选时根据传入的cateid,先查出这个分类的floor,然后查出他的所有上级分类id
剩下就是把所有得到的id作为条件筛选了
这里有个关键,要建一个BTREE索引,以cate到cateN的顺序都包括进来
同数据量方法2要快很多,但他的缺点是子分类移动时还要改产品表
我用的是无限极分类,子分类层次不一定的,子分类下面可能还有子分类,所以我最先想到的是递归,后来觉得要从数据库设计入手,就在分类表加了一个路径字段
如果你一开始是全部取出的话,那么就不存在“点击一个分类,把当前分类及其所有子分类的所有产品列出来”这么一说了
既然是点击才出来,那自然是出来的是一级子分类,再点再出。这样才是统一的风格
就是你一次性全取出来,也是按父分类指示的一级一级的读
不需要递归啊,你可以到上面一个表里“层次结构”那个字段里面找所点击分类的分类号,凡是有的都是子孙后代分类了。
比如说点击的分类,id号为18,就可以用cate_path like "%18-%" or cate_path like "%-18%"来筛选。
无限级这么关键的词你居然没说!
我觉得思路还是4楼的第二种方法,把欲查询的cateid的所有父分类id也都折腾出来,合并到一起去查
用 , 或者 - 应该都可以吧
产品的cateid字段保存这样的格式:1,5,8,21 是从1级分类到N级分类
如果想查cateid=8的所有产品,那先找出5和1这2个上级分类,生成字符串1,5,8
然后查询语句用 cateid like '$cateid %',当然这个字段单独加个索引
补充下
如果想查cateid=8的所有产品,那先找出5和1这2个上级分类,生成字符串1,5,8
这一步的字符串可以写在数据库中,也可以把所有分类id对应的查询字符串缓存起来,实际运行时间可以跳过了^ ^
我觉得你的筛选方式会出问题的。如果是id为8的呢?"%8%"这样的表达式会把18或者81、82等等都包含进去的。
这个方法再把表达式改一下就好了啊。如果id为0的也可能被点击到的话,最后in里面除了搜索出来的之外,就得多包含一个当前点击的id自己。否则都不需要加别的了,因为只有0才会前后都不带“-”出现。
无限级这么关键的词你居然没说!
我觉得思路还是4楼的第二种方法,把欲查询的cateid的所有父分类id也都折腾出来,合并到一起去查
用 , 或者 - 应该都可以吧
产品的cateid字段保存这样的格式:1,5,8,21 是从1级分类到N级分类
如果想查cateid=8的所有产品,那先找出5和1这2个上级分类,生成字符串1,5,8
然后查询语句用 cateid like '$cateid %',当然这个字段单独加个索引
用-还是用,没有区别啊,而且也不需要查找父分类。
这个简单。就一直查询下去。
由主分类ID查询出子分类,又从子分类ID查询出第三级,又从第三级ID查询出第四级……
当然,你的服务器能经得起这样折腾才行。
不管你是不是无限级,都是这样的查询。没有捷径可走。
这个简单。就一直查询下去。
由主分类ID查询出子分类,又从子分类ID查询出第三级,又从第三级ID查询出第四级……
当然,你的服务器能经得起这样折腾才行。
不管你是不是无限级,都是这样的查询。没有捷径可走。
他有一个cate_path字段啊,不需要这样的。
这个方法再把表达式改一下就好了啊。如果id为0的也可能被点击到的话,最后in里面除了搜索出来的之外,就得多包含一个当前点击的id自己。否则都不需要加别的了,因为只有0才会前后都不带“-”出现。
兄台想的果然周全 180 也包含18,确实是要加杠杠
刚在公司没时间细看
你们讨论的是在分类上加path,然后查询产品的sql语句用in?
无限级分类用in那效率会不会...in一大堆ID
还好in可以用到索引,不知道mysql有没有为in排序,否则索引树要从头到底遍历一次,然后复杂度还要乘以id的个数
我在4楼和10楼说的都是加速查询的方案
只是实现的话怎么都有办法
获得cateid后从内存缓存中(编辑分类时一次性生成)用php的变量hash读取他的分类路径,这一步几乎不耗时间
然后直接用路径字符串借助索引树一次性定位到全部数据集,速度很快
很抱歉天气热有点浮躁,如果有错误请指正

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



In iOS 17, you can set multiple timers on your iPhone using the Clock app, or use Siri to set it hands-free. We discuss both in this article. Let's take a look at them. Set Multiple Timers on iPhone Using the Clock App Open the Clock app on your iPhone and tap the Timers tab in the lower right corner. Now, set the hours, minutes, and seconds. You can use the "Label" and "When does the timer end" options to set a name for the timer and a preferred tone when the timer completes. This will help you differentiate between timers. Once done, click the "Start" button. Then, click the "+" icon in the upper right corner. Now, repeat the above steps to set multiple timers on iPhone. You can also browse

Classification and Usage Analysis of JSP Comments JSP comments are divided into two types: single-line comments: ending with, only a single line of code can be commented. Multi-line comments: starting with /* and ending with */, you can comment multiple lines of code. Single-line comment example Multi-line comment example/**This is a multi-line comment*Can comment on multiple lines of code*/Usage of JSP comments JSP comments can be used to comment JSP code to make it easier to read

How to Make a GroceryList on iPhone in iOS17 Creating a GroceryList in the Reminders app is very simple. You just add a list and populate it with your items. The app automatically sorts your items into categories, and you can even work with your partner or flat partner to make a list of what you need to buy from the store. Here are the full steps to do this: Step 1: Turn on iCloud Reminders As strange as it sounds, Apple says you need to enable reminders from iCloud to create a GroceryList on iOS17. Here are the steps for it: Go to the Settings app on your iPhone and tap [your name]. Next, select i

Product parameters refer to the meaning of product attributes. For example, clothing parameters include brand, material, model, size, style, fabric, applicable group, color, etc.; food parameters include brand, weight, material, health license number, applicable group, color, etc.; home appliance parameters include brand, size, color , place of origin, applicable voltage, signal, interface and power, etc.

After the release of Xiaomi 14Ultra, many friends who like to take pictures have chosen to place orders. Xiaomi 14Ultra provides more choices, such as the photo mirror function, and you can choose to turn on the "photo mirror rotation" function. In this way, when you take photos, you can take selfies in the way you are used to. But how should Xiaomi 14Ultra set up the camera mirror? How to set up camera mirroring on Xiaomi Mi 14Ultra? 1. Open the camera of Xiaomi 14Ultra. 2. Find "Settings" on the screen. 3. On this page, you will see an option labeled "Capture Settings." 4. Click this option, and then find the "Photo Mirror" option in the drop-down menu. 5. Just open it. Xiaomi 14U
![How to set up my business card in Contacts on iPhone [2023]](https://img.php.cn/upload/article/000/465/014/169538863364905.jpg?x-oss-process=image/resize,m_fill,h_207,w_330)
With My Business Cards in iOS, you can create a personalized contact card that Siri and other services recognize and associate with you and your phone number. With the introduction of contact posters in iOS17, My Cards becomes very important as it is now used to create your contact posters. If you're eager to get your contact poster up and running, you have to start by setting up My Business Card. We'll walk through how to create a My Business Card and how to make it work smoothly with Siri and your contact poster. How to Set Up My Business Cards in Contacts on iPhone [2023] If you are setting up My Business Cards on your iPhone for the first time, you must do it through the Contacts app only
![How to turn off alarms on iPhone [2023]](https://img.php.cn/upload/article/000/465/014/169259550735100.png?x-oss-process=image/resize,m_fill,h_207,w_330)
Since the advent of smartphones, they have undoubtedly replaced alarm clocks. If you own an iPhone, you can use the Clock app to easily set as many alarms for multiple occasions throughout the day. The app lets you configure the alarm time, the tone, how often it repeats, and whether you want to delay them using the Snooze option. If you want to turn off the alarm you have set, the following post should help you disable and delete regular alarms and wake-up alarms on your iPhone. How to Turn Off a Regular Alarm on iPhone By default, when you add an alarm on the Clock app or ask Siri to add an alarm for you, you're actually creating a regular alarm. You can create as many alarm clocks on your iPhone as you like and put them

iOS17 is finally here, and it includes a lot of new features. Let's learn how to change contact photos on iPhone in today's tutorial. Apple's recent WWDC 2023 event unveiled a slew of exciting products and upcoming software updates. One of the notable features of iOS 17 is the option to customize your contact photos and posters, providing a unique way to greet others when they receive a call from you on their iPhone. This innovative feature for iOS is designed to make phone calls more personal and user-friendly, allowing you to choose how they appear on the recipient's screen. If you're eager to get your hands on this new feature and greet your loved ones in a personalized way when making calls, here's how to add it to your iPhone
