上面是分类表,产品表就是 关联 分类表的 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读取他的分类路径,这一步几乎不耗时间
然后直接用路径字符串借助索引树一次性定位到全部数据集,速度很快
很抱歉天气热有点浮躁,如果有错误请指正

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python are both high-level programming languages that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.


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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version
Visual web development tools

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

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version
Useful JavaScript development tools