Php Master-phpnewnew

Source: Internet
Author: User
Ask the php Master --- phpnewnew I have two data tables, table a has the titleb field and table titlea has 1000 data records. Table B has 10 data requirements: query the title of Table, if. title in B. title,. store the title to $ array1. if sizeof ($ array1) & gt; ask the php Master --- phpnewnew
I have 2 data tables

Table
Field title

Table B
There is also a field title


Table a has 1000 data records, and Table B has 10 data records.

Requirements:

Query the title of Table a. If a. title is in Table B. title, a. title is saved to $ array1. if sizeof ($ array1)> 5, it is not saved.

If a. title is not in B. title, a. title is saved to $ array2. if sizeof ($ array2)> 5, it is not saved.

My practices
PHP code
  $sql="select title from a"........while($tpl2result=mysql_fetch_array($result_sql))          {              if(sizeof($array1)<5)              {               if($this->guolv($tpl2result['title']))                 array_push($array1,$tpl2result);                        }             if(sizeof($array2)<5)             {             if(!$this->guolv($tpl2result['title']))                 array_push($array2,$tpl2result);             }             if(sizeof($newtalk)==5&&sizeof($newvote)==5)             {                 break;                     exit;             }          }


Questions:

This method greatly wastes resources, slows down, and learns from Masters ..


------ Solution --------------------
You can google the join operation of SQL statements.
Select * from a left join B on a. title = B. title
Select * from a right join B on a. title = B. title

This eliminates the need for SQL statements to compare the two tables.

In databases, join is highly efficient.
------ Solution --------------------
Yes. You only need a. title table, and left join once is enough. if it is NULL, it is not in Table B.
SQL len
Or php.
------ Solution --------------------
Select title from a left join B on a. title = B. title limit 0, 10
It seems that the limit is 10.
------ Solution --------------------
When I just woke up, the master will stop calling it. you know it, and I know it.

The code has not gone through practice. just as a train of thought, the table must be indexed. However, I have not tested index using the title, and I have little knowledge about Mysql than php.

PHP code
$ Array1 = array (); $ SQL = "SELECT. title As atitle FROM a WHERE. 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. title As atitle FROM a WHERE. title not in (SELECT B. title FROM B) "; $ total = 0; while ($ result = $ mysql-> fetch_assoc () {$ array2 [] = $ result ['atitle']; if ($ total> 5) {break;} $ total ++ ;}
------ Solution --------------------

Discussion

Why are you talking about it?
Besides, I still got an error.

------ Solution --------------------
You just need B LEFT JOIN. Dominated by B.

Discussion

Reference:

Select title from a left join B on a. title = B. title limit 0, 10
It seems that the limit is 10.


If none of the first 995 entries are in B, but 996---1000 is in B, it seems that it doesn't work ,,

It seems that there is no proper solution to this kind of requirement. it can only be implemented on the first floor, and the efficiency is higher than that of mine.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.