Mysql multi-condition query

Source: Internet
Author: User
Mysql multi-condition query problem "select * from subcatalog WHERE name LIKE '% $ keywords %' and catalog = $ catalog order by rank $ rank limit $ pages, $ rows"


The preceding statement can be used to search for other data when the value of $ catalog is null. now, when the value of $ catalog is null, nothing is available in the list.


Reply to discussion (solution)

When $ catalog is empty
Catalog = $ catalog ORDER
It becomes
Catalog = ORDER
Apparently, a syntax error occurs.

Enclose $ catalog in quotation marks and write '$ catalog'

However, there is still a problem: only records with the catalog empty can be queried.
If you do not want this, do not write catalog = '$ catalog' when $ catalog is empty'

The and catalog = $ catalog condition is proposed to write a variable separately. when $ catalog is empty, the entire condition is empty...

The syntax is adjusted as follows:

If ($ catalog = '') {$ tiaojian ='';} else {$ tiaojian = "and catalog = $ catalog ";} $ SQL = "select * from subcatalog WHERE name LIKE '% $ keywords %' $ tiaojian ORDER BY rank $ rank limit $ pages, $ rows"

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.