一个查询语句怎么写,mysql php
mysql php
数据库表结构产品表 product
产品id 产品目录 产品名称
productid cids productname
1 (1,3,4) 精品服装1
2 (3,8) 精品服装2
3 (7,4,9) 精品服装3
需求:
1、比如进入一个产品的页面,通过url得到productid,如何通过productid 得到cids
现需要写个语句,找到product表中,与cids 有相同的所有产品
比如通过url得到了productid值为1,那么cids值为(1,3,4),要找出product表中只要含有 1或3或4的所有产品
回复讨论(解决方案)
将 1,3,4 分割成数组,然后遍历查询。
你这个要求有几个问题,首先说明数据库表设计的完全不对,要是查询出的结果还要拆分,拆完了还要like的查询那么就应该重新设计这个表,比如一行只记一个cids。
如果你一定要进行现在这样的操作,我建议你分两步走,不然会非常难写,第一步先
select cids from product where productid = xxxx
得到的结果,比如是(1,3,4)在PHP中拆成数组,然后数据库中查询
select productid from product where ','+cids+',' like '%,1,%'unionselect productid from product where ','+cids+',' like '%,3,%'unionselect productid from product where ','+cids+',' like '%,4,%'
不直接like,而前后加逗号的原因是如果是12,13,14也会like1成功
另外这个写法如果表大了会很慢,因为会全表扫描,所以我还是建议你把表结构改一下。
select productid, productnamefrom product where find_in_set((select cids from product where productid = '$productid'), cids)
未经测试,如果不行,则先根据$productid查询出$cids,然后用$cids代替find_in_set函数的第一个参数即可。
如2楼所说,如果表记录很大,那么可能会很慢,建议还是改一下表结构。把productid和cid用单独一个表来记录对应关系,做好索引。
请将 cids 两端的圆括号去掉
即形如 (1,3,4) 的改为形如 1,3,4 的
然后使用自连接,连接条件使用 find_in_set 函数计算
select T1.* from product T1, product T2 where find_in_set(T1.productid, T2.cids) and 2.productid=1

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

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



Alipay PHP...

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.
