Table of Contents
回复讨论(解决方案)
Home Backend Development PHP Tutorial 分不多,只有100,希望大家可以帮我

分不多,只有100,希望大家可以帮我

Jun 20, 2016 pm 12:44 PM

A表: 关键字段     id(字段id)    attr_type(字段类别 只获取是1)
                                    15              1
                                    14              0
                                    13              1
                                    12              1             可能会有更多
B表: 关键字段     a_id(字段id)   in_id(同A表ID 关联)shop_id(获取依据字段 id)
                                    400              15                                 15
                                    399              15                                 15

                                    388              14                                 15-----
                                    387              14                                 15-----这两组不做组合 以为A表中 type是0

                                    386              13                                 15
                                    385              13                                 15

                                    384              12                                 15
                                    383              12                                 15        可能会有更多是1的属性 也可能就一组

                                    384              12                                 14
                                    383              12                                 14 
                                    382              12                                 14
                                    381              12                                 14
                                    380              12                                 14        可能会有更多
C表:关键字段     id(字段id)   shop_id(同B表shop_id 关联)attr_id(循环出来的数值)   以下是我想得到的结果
                                      1              15                                          400|386|384
                                      2              15                                          400|386|385   
                                      3              15                                          400|387|384   如果此组已存在,就跳过 如果不存在 往下自动循环出来写入

意图 我给予ID 15的指令  从AB表获取数据 生成到C表 如果原先已存在,就忽略不生成不存在的生成





比如我想循环shop_id  15的这个依据id 生成到C表 求代码
以下是我写的部分sql 后面不会了 求救


    $sql = "SELECT b.a_id ".            "FROM " .$GLOBALS['ecs']->table('B'). " AS b ".            "LEFT JOIN " .$GLOBALS['ecs']->table('A'). " AS a ".            "ON a.id IN(b.in_id) ".            "WHERE b.shop_id = '15' AND a.attr_type = '1' " .             "ORDER BY b.a_id DESC";    $row = $GLOBALS['db']->getAll($sql);
Copy after login















回复讨论(解决方案)

没看明白结果是怎么来的,猜测一下

create temporary table A (id int, attr_type int);insert into A values (15, 1),(14, 0),(13, 1),(12, 1); create temporary table B (a_id int, in_id int, shop_id int);insert into B values (400, 15, 15),(399, 15, 15),(388, 14, 15)      ,(387, 14, 15),(386, 13, 15),(385, 13, 15),(384, 12, 15)      ,(383, 12, 15),(384, 12, 14),(383, 12, 14)       ,(382, 12, 14),(381, 12, 14),(380, 12, 14);select shop_id, group_concat(a_id) as attr_id from A, B where A.id=B.in_id and A.attr_type=1 group by shop_id, B.in_id
Copy after login
shop_id attr_id 14      384,383,382,381,380 15      384,383 15      386,385 15      400,399 
Copy after login

结果就是 我浏览器给与 ....&shop_id=15 这个格局 php收到指令 从b表获取shop_id为15的数据 然后从b表隔离掉 attr_type 不是1的数据
获取到的应该是 以下几组
B表: 关键字段 a_id(字段id) in_id(同A表ID 关联)shop_id(获取依据字段 id)
400 15 15
399 15 15
386 13 15
385 13 15
384 12 15
383 12 15

然后归类 以 in_id 区分

400 15 15
399 15 15
*********************************************************************************************************************一个分组
386 13 15
385 13 15
*********************************************************************************************************************一个分组
384 12 15
383 12 15
*********************************************************************************************************************一个分组

然后把 前面的 a_id 字段数据做组合变化 写入到c表 形成 400|386|384 这种

C表:关键字段 id(字段id) shop_id(同B表shop_id 关联)attr_id(循环出来的数值) 以下是我想得到的结果
1 15 400|386|384
2 15 400|386|385

C表 id 为 auto属性 shop_id 为我给与的id attr_id 就是组合出来的变化结果

结果就是 我浏览器给与 ....&shop_id=15 这个格局 php收到指令 从b表获取shop_id为15的数据 然后从b表隔离
写错了 应该是从A表帅选
结果就是 我浏览器给与 ....&shop_id=15 这个格局 php收到指令 从b表获取shop_id为15的数据 然后从 A表隔离

select in_id, shop_id, group_concat(a_id) as attr_id from A, B where A.id=B.in_id and A.attr_type=1 and shop_id=15 group by  B.in_id
Copy after login
Copy after login
in_id shop_id attr_id 12    15      384,383 13    15      386,385 15    15      400,399 
Copy after login
Copy after login

select in_id, shop_id, group_concat(a_id) as attr_id from A, B where A.id=B.in_id and A.attr_type=1 and shop_id=15 group by  B.in_id
Copy after login
Copy after login
in_id shop_id attr_id 12    15      384,383 13    15      386,385 15    15      400,399 
Copy after login
Copy after login


版主,我已经自己写好了,谢谢你啊


大神,请您帮个忙,怎么把这个表里的数据按照我的要求隔离出来
img_id 标示为1的 goods_id 261 attr_id 区分组的依据

最终的要求就是 输出
a= '584,583,582,581,580,579'
b= '578,577,576,575,574,573'

572和495不输出 因为img_id 不等于一 谢谢了

select goods_id, attr_id, groub_concat(goods_attr_id) from tbl_name where img_id=1 group by attr_id

$a = array('571','570');$b = array('569','568','567','566','565','564');$c = array('563','562','561','560','559');foreach(combineDika($a,$b) as $v){	$r[] = join('|', $v);}$attr_all =  join(',', $r);$attr_all =  explode(',', $attr_all);foreach ($attr_all as $list){        $sql = "SELECT goods_attr " .                "FROM " .$GLOBALS['ecs']->table('products') .                "WHERE goods_attr = '".$list."' ";        $check_attr = $GLOBALS['db']->getOne($sql);		$value = 'ECS000261g_p'.rand(10000,99999);		if (empty($check_attr))        {             $sql = "INSERT INTO " . $GLOBALS['ecs']->table('products') . " (goods_id, goods_attr, product_sn, product_number)  VALUES ('261', '".$list."', '$value', '10000')";             $GLOBALS['db']->query($sql);        }}
Copy after login

这是我后面执行的代码

用个2B方法读取出来了
谢谢版主
我现在结贴

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1670
14
PHP Tutorial
1274
29
C# Tutorial
1256
24
Explain secure password hashing in PHP (e.g., password_hash, password_verify). Why not use MD5 or SHA1? Explain secure password hashing in PHP (e.g., password_hash, password_verify). Why not use MD5 or SHA1? Apr 17, 2025 am 12:06 AM

In PHP, password_hash and password_verify functions should be used to implement secure password hashing, and MD5 or SHA1 should not be used. 1) password_hash generates a hash containing salt values ​​to enhance security. 2) Password_verify verify password and ensure security by comparing hash values. 3) MD5 and SHA1 are vulnerable and lack salt values, and are not suitable for modern password security.

PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

How does PHP type hinting work, including scalar types, return types, union types, and nullable types? How does PHP type hinting work, including scalar types, return types, union types, and nullable types? Apr 17, 2025 am 12:25 AM

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.

The Enduring Relevance of PHP: Is It Still Alive? The Enduring Relevance of PHP: Is It Still Alive? Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

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 and Python: Code Examples and Comparison PHP and Python: Code Examples and Comparison Apr 15, 2025 am 12:07 AM

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

How do you prevent SQL Injection in PHP? (Prepared statements, PDO) How do you prevent SQL Injection in PHP? (Prepared statements, PDO) Apr 15, 2025 am 12:15 AM

Using preprocessing statements and PDO in PHP can effectively prevent SQL injection attacks. 1) Use PDO to connect to the database and set the error mode. 2) Create preprocessing statements through the prepare method and pass data using placeholders and execute methods. 3) Process query results and ensure the security and performance of the code.

See all articles