Table of Contents
回复讨论(解决方案)
Home Backend Development PHP Tutorial 求查询优化方法

求查询优化方法

Jun 23, 2016 pm 02:01 PM

请问下面的代码有优化的方法吗

$catalog_1 = "select name from commodity where catalog = 1 ORDER BY rank desc LIMIT 10";$result_1 = mysql_query($catalog_1);while($row_1 = mysql_fetch_assoc($result_1)){     $response [] = $row_1;}$catalog_2 = "select name from commodity where catalog = 2 ORDER BY rank desc LIMIT 10";$result_2 = mysql_query($catalog_2);while($row_2 = mysql_fetch_assoc($result_2)){     $response [] = $row_2;}$catalog_3 = "select name from commodity where catalog = 3 ORDER BY rank desc LIMIT 10";$result_3 = mysql_query($catalog_3);while($row_3 = mysql_fetch_assoc($result_3)){     $response [] = $row_3;}$catalog_4 = "select name from commodity where catalog = 4 ORDER BY rank desc LIMIT 10";$result_4 = mysql_query($catalog_4);while($row_4 = mysql_fetch_assoc($result_4)){     $response [] = $row_4;}$catalog_5 = "select name from commodity where catalog = 5 ORDER BY rank desc LIMIT 10";$result_5 = mysql_query($catalog_5);while($row_5 = mysql_fetch_assoc($result_5)){     $response [] = $row_5;}$catalog_6 = "select name from commodity where catalog = 6 ORDER BY rank desc LIMIT 10";$result_6 = mysql_query($catalog_6);while($row_6 = mysql_fetch_assoc($result_6)){     $response [] = $row_6;}$catalog_7 = "select name from commodity where catalog = 7 ORDER BY rank desc LIMIT 10";$result_7 = mysql_query($catalog_7);while($row_7 = mysql_fetch_assoc($result_7)){     $response [] = $row_7;}$catalog_8 = "select name from commodity where catalog = 8 ORDER BY rank desc LIMIT 10";$result_8 = mysql_query($catalog_8);while($row_8 = mysql_fetch_assoc($result_8)){     $response [] = $row_8;}$catalog_11 = "select name from commodity where catalog = 11 ORDER BY rank desc LIMIT 10";$result_11 = mysql_query($catalog_11);while($row_11 = mysql_fetch_assoc($result_11)){     $response [] = $row_11;}$catalog_10 = "select name from commodity where catalog = 10 ORDER BY rank desc LIMIT 10";$result_10 = mysql_query($catalog_10);while($row_10 = mysql_fetch_assoc($result_10)){     $response [] = $row_10;}
Copy after login


回复讨论(解决方案)

function getCataLog($catalog){	$catalog = "select name from commodity where catalog = ".$catalog." ORDER BY rank desc LIMIT 10";	$result = mysql_query($catalog); 	while($row = mysql_fetch_assoc($result)){	     $response [] = $row;	}}
Copy after login

改写成方法

是这样调用吗 getCataLog(11);

但没数据出来

是这样调用吗 getCataLog(11);

但没数据出来

function getCataLog($catalog){    $catalog = "select name from commodity where catalog = ".$catalog." ORDER BY rank desc LIMIT 10";    $result = mysql_query($catalog);      while($row = mysql_fetch_assoc($result)){         $response [] = $row;    }    return $response;}
Copy after login

还有个神奇的问题

catalog = 11 数据库有3条记录
catalog = 12 数据库有11条记录

$responses = getCataLog(11) + getCataLog(12);

这样输出应该是有14条记录的,但实标只输出了10条。而且catalog = 12的数据是从第四条开始接的

不是这么用的 php里面的 + 相比于数组的合并 array_merge 相同键值的被覆盖掉了

select name,catalog,rank from commodity c where 10 > (select count(*) from commodity where catalog=c.catalog and id>c.id) and catalog in (1,2,3,4,5,6,7,8,10,11) order by c.catalog,c.rank desc
Copy after login
Copy after login

function getCataLog($catalog,&$response = array()){    $catalog = "select name from commodity where catalog = ".$catalog." ORDER BY rank desc LIMIT 10";    $result = mysql_query($catalog);      while($row = mysql_fetch_assoc($result)){         $response [] = $row;    }}$response = array();getCataLog(1,$response);var_dump($response);getCataLog(2,$response);var_dump($response);
Copy after login

select name,catalog,rank from commodity c where 10 > (select count(*) from commodity where catalog=c.catalog and id>c.id) and catalog in (1,2,3,4,5,6,7,8,10,11) order by c.catalog,c.rank desc
Copy after login
Copy after login


请问再加两个条件怎么加啊,我这样加了,但这样加了和数据库对不上,少了很多条数据。

$marketId = $_GET['marketId'];$cityId = $_GET['cityId'];"select name from commodity where catalog = 1 and cityId = $cityId and marketId = $marketId ORDER BY rank desc LIMIT 10""select name,catalog,rank from commodity c where 10 > (select count(*) from commodity where catalog=c.catalog and id>c.id) and catalog in (1,2,3,4,5,6,7,8,10,11) and cityId = $cityId and marketId = $marketId order by c.catalog,c.rank desc"
Copy after login

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
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)

11 Best PHP URL Shortener Scripts (Free and Premium) 11 Best PHP URL Shortener Scripts (Free and Premium) Mar 03, 2025 am 10:49 AM

11 Best PHP URL Shortener Scripts (Free and Premium)

Introduction to the Instagram API Introduction to the Instagram API Mar 02, 2025 am 09:32 AM

Introduction to the Instagram API

Working with Flash Session Data in Laravel Working with Flash Session Data in Laravel Mar 12, 2025 pm 05:08 PM

Working with Flash Session Data in Laravel

Build a React App With a Laravel Back End: Part 2, React Build a React App With a Laravel Back End: Part 2, React Mar 04, 2025 am 09:33 AM

Build a React App With a Laravel Back End: Part 2, React

Simplified HTTP Response Mocking in Laravel Tests Simplified HTTP Response Mocking in Laravel Tests Mar 12, 2025 pm 05:09 PM

Simplified HTTP Response Mocking in Laravel Tests

cURL in PHP: How to Use the PHP cURL Extension in REST APIs cURL in PHP: How to Use the PHP cURL Extension in REST APIs Mar 14, 2025 am 11:42 AM

cURL in PHP: How to Use the PHP cURL Extension in REST APIs

12 Best PHP Chat Scripts on CodeCanyon 12 Best PHP Chat Scripts on CodeCanyon Mar 13, 2025 pm 12:08 PM

12 Best PHP Chat Scripts on CodeCanyon

Announcement of 2025 PHP Situation Survey Announcement of 2025 PHP Situation Survey Mar 03, 2025 pm 04:20 PM

Announcement of 2025 PHP Situation Survey

See all articles