一、问题
产品表
有平台id 产品名称
id ptid name
1 100 产品1
2 100 产品2
3 100 产品3
4 100 产品4
5 100 产品5
6 100 产品6
7 101 产品7
8 101 产品8
9 101 产品9
10 101 产品10
11 101 产品11
如何根据某个平台id 只取这个平台的3条数据(顺序可以随机)
id ptid name
1 100 产品1
2 100 产品2
3 100 产品3
7 101 产品7
8 101 产品8
9 101 产品9
二、建表
create table p_a( id int, ptid varchar(20), name varchar(20) ) insert into p_a(id,ptid,name) values(1,'100','产品1'); insert into p_a(id,ptid,name) values(2,'100','产品2'); insert into p_a(id,ptid,name) values(3,'100','产品3'); insert into p_a(id,ptid,name) values(4,'100','产品4'); insert into p_a(id,ptid,name) values(5,'100','产品5'); insert into p_a(id,ptid,name) values(6,'100','产品6'); insert into p_a(id,ptid,name) values(7,'101','产品7'); insert into p_a(id,ptid,name) values(8,'101','产品8'); insert into p_a(id,ptid,name) values(9,'101','产品9'); insert into p_a(id,ptid,name) values(10,'101','产品10'); insert into p_a(id,ptid,name) values(11,'101','产品11');
三、sql
select * from ( select a.*, case when @oldptid=ptid then @lagfield:=@lagfield+1 else @lagfield:=1 end rn, @oldptid:=ptid from p_a a,(select @lagfield:=0,@oldptid:='') r ) a where rn<=3
四、总结
MySQL根据ptid分组,分别对分组内进行标注序号,然后根据序号取出各分组内的记录

The article discusses using MySQL's ALTER TABLE statement to modify tables, including adding/dropping columns, renaming tables/columns, and changing column data types.

Article discusses configuring SSL/TLS encryption for MySQL, including certificate generation and verification. Main issue is using self-signed certificates' security implications.[Character count: 159]

Article discusses strategies for handling large datasets in MySQL, including partitioning, sharding, indexing, and query optimization.

Article discusses popular MySQL GUI tools like MySQL Workbench and phpMyAdmin, comparing their features and suitability for beginners and advanced users.[159 characters]

The article discusses dropping tables in MySQL using the DROP TABLE statement, emphasizing precautions and risks. It highlights that the action is irreversible without backups, detailing recovery methods and potential production environment hazards.

Article discusses using foreign keys to represent relationships in databases, focusing on best practices, data integrity, and common pitfalls to avoid.

The article discusses creating indexes on JSON columns in various databases like PostgreSQL, MySQL, and MongoDB to enhance query performance. It explains the syntax and benefits of indexing specific JSON paths, and lists supported database systems.

Article discusses securing MySQL against SQL injection and brute-force attacks using prepared statements, input validation, and strong password policies.(159 characters)


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

Dreamweaver CS6
Visual web development tools

WebStorm Mac version
Useful JavaScript development tools

Zend Studio 13.0.1
Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.