Home Backend Development PHP Tutorial 请问php大师-phpnewnew

请问php大师-phpnewnew

Jun 13, 2016 am 10:08 AM
array from select title

请教php大师---phpnewnew
我有2张数据表

a表
有 字段title

b表
也有字段title


a表中有1000条数据,b表中有10条数据

要求:

查询a表的title,若a.title 在 b.title中,则a.title存入 $array1中,如果sizeof($array1)>5 ,则不存。

若 若a.title 不在 b.title中,则 则a.title存入 $array2中,如果sizeof($array2)>5 ,则不存。

我的做法

PHP code
<!--Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->$sql="select title from a"........while($tpl2result=mysql_fetch_array($result_sql))          {              if(sizeof($array1)guolv($tpl2result['title']))                 array_push($array1,$tpl2result);                        }             if(sizeof($array2)guolv($tpl2result['title']))                 array_push($array2,$tpl2result);             }             if(sizeof($newtalk)==5&&sizeof($newvote)==5)             {                 break;                     exit;             }          }
Copy after login


questions:

 这种做法极大地浪费了资源,减慢速度,向大师取经。。


------解决方案--------------------
可以google一下sql语句的 join 操作
select * from a left join b on a.title = b.title
select * from a right join b on a.title = b.title

这样就不需要sql语句来对比两张表了

在数据库中,join的效率是很高的
------解决方案--------------------
就是嘛。你只要a.title表, LEFT JOIN 一次就够了,为NULL的就是不在b表咯。
长度sql len
或php判断下咯。
------解决方案--------------------
select title from a left join b on a.title=b.title limit 0,10
貌似这样是限制在10条内了
------解决方案--------------------
刚睡醒,大师就别叫了,这个就不扯了,你心知肚明,我也知道.

代码没有经过实战,仅仅做为思路,表必须做好索引,不过用title做索引我没有做过测试,我的Mysql 比php掌握得还少.

PHP code
$array1 = array();$SQL = "SELECT a.title As atitle FROM a WHERE a.title in(SELECT b.title FROM b)";$mysql->query($SQL);$total = 0;while ($result = $mysql->fetch_assoc()) {    $array[] = $result['atitle'];    if ($total > 5) {        break;    }    $total++;}$array2 = array();$SQL = "SELECT a.title As atitle FROM a WHERE a.title not in(SELECT b.title FROM b)";$total = 0;while ($result = $mysql->fetch_assoc()) {    $array2[] = $result['atitle'];    if ($total > 5) {        break;    }    $total++;}<br><font color="#e78608">------解决方案--------------------</font><br><br>
Copy after login
探讨

人家又没问你们,干嘛多嘴呀?
何况还答错了

------解决方案--------------------
你就b LEFT JOIN a 就好了。以b为主。

探讨

引用:

select title from a left join b on a.title=b.title limit 0,10
貌似这样是限制在10条内了


如果前面995条都不在b里面,,从996---1000在b里面,,似乎不行了吧,,

碰到这种需求貌似没有妥善的解决方法,,只能按1楼的搞了,效率比我那个高
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 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
1653
14
PHP Tutorial
1251
29
C# Tutorial
1224
24
How to hide the select element in jquery How to hide the select element in jquery Aug 15, 2023 pm 01:56 PM

How to hide the select element in jquery: 1. hide() method, introduce the jQuery library into the HTML page, you can use different selectors to hide the select element, the ID selector replaces the selectId with the ID of the select element you actually use; 2. css() method, use the ID selector to select the select element that needs to be hidden, use the css() method to set the display attribute to none, and replace selectId with the ID of the select element.

Sort array using Array.Sort function in C# Sort array using Array.Sort function in C# Nov 18, 2023 am 10:37 AM

Title: Example of using the Array.Sort function to sort an array in C# Text: In C#, array is a commonly used data structure, and it is often necessary to sort the array. C# provides the Array class, which has the Sort method to conveniently sort arrays. This article will demonstrate how to use the Array.Sort function in C# to sort an array and provide specific code examples. First, we need to understand the basic usage of the Array.Sort function. Array.So

Asynchronous processing method of Select Channels Go concurrent programming using golang Asynchronous processing method of Select Channels Go concurrent programming using golang Sep 28, 2023 pm 05:27 PM

Asynchronous processing method of SelectChannelsGo concurrent programming using golang Introduction: Concurrent programming is an important area in modern software development, which can effectively improve the performance and responsiveness of applications. In the Go language, concurrent programming can be implemented simply and efficiently using Channels and Select statements. This article will introduce how to use golang for asynchronous processing methods of SelectChannelsGo concurrent programming, and provide specific

How to implement change event binding of select elements in jQuery How to implement change event binding of select elements in jQuery Feb 23, 2024 pm 01:12 PM

jQuery is a popular JavaScript library that can be used to simplify DOM manipulation, event handling, animation effects, etc. In web development, we often encounter situations where we need to change event binding on select elements. This article will introduce how to use jQuery to bind select element change events, and provide specific code examples. First, we need to create a dropdown menu with options using labels:

Simple and clear method to use PHP array_merge_recursive() function Simple and clear method to use PHP array_merge_recursive() function Jun 27, 2023 pm 01:48 PM

When programming in PHP, we often need to merge arrays. PHP provides the array_merge() function to complete array merging, but when the same key exists in the array, this function will overwrite the original value. In order to solve this problem, PHP also provides an array_merge_recursive() function in the language, which can merge arrays and retain the values ​​of the same keys, making the program design more flexible. array_merge

How to use the array_combine function in PHP to combine two arrays into an associative array How to use the array_combine function in PHP to combine two arrays into an associative array Jun 26, 2023 pm 01:41 PM

In PHP, there are many powerful array functions that can make array operations more convenient and faster. When we need to combine two arrays into an associative array, we can use PHP's array_combine function to achieve this operation. This function is actually used to combine the keys of one array as the values ​​of another array into a new associative array. Next, we will explain how to use the array_combine function in PHP to combine two arrays into an associative array. Learn about array_comb

what does title mean what does title mean Aug 04, 2023 am 11:18 AM

Title is the meaning that defines the title of the web page. It is located within the tag and is the text displayed in the title bar of the browser. Title is very important for the search engine optimization and user experience of the web page. When writing HTML web pages, you should pay attention to using relevant keywords and attractive descriptions to define the title element to attract more users to click and browse.

What is the reason why Linux uses select? What is the reason why Linux uses select? May 19, 2023 pm 03:07 PM

Because select allows developers to wait for multiple file buffers at the same time, it can reduce IO waiting time and improve the IO efficiency of the process. The select() function is an IO multiplexing function that allows the program to monitor multiple file descriptors and wait for one or more of the monitored file descriptors to become "ready"; the so-called "ready" state is Refers to: the file descriptor is no longer blocked and can be used for certain types of IO operations, including readable, writable, and exceptions. select is a computer function located in the header file #include. This function is used to monitor file descriptor changes—reading, writing, or exceptions. 1. Introduction to the select function. The select function is an IO multiplexing function.

See all articles