Can the SQL statement directly regard the intercepted string as the where condition?

Source: Internet
Author: User
Can the SQL statement directly regard the intercepted string as the where condition MySQL?

For example
Select 'id' from 'table' where left ('flag', 1) = 1;


I have a string named flag used to indicate various attributes of the product, 10-digit Int, such as 1100000000.
For example, the first digit 1 indicates that the commodity price range is between and RMB.
Then I want to find the first item with the number 1 in the flag from the table.
Can this be done? just use mysql


Reply to discussion (solution)

Select 'id' from 'table' where left ('flag', 1) = '1'

Isn't your SQL ??

Dizzy... I felt like I wrote it like this. I tried it a few times but it didn't work.
The where condition is incorrect.

In addition, I would like to ask, how is the performance of such SQL statements?

Select 'id' from 'table' where left ('flag', 1) = '1'

How is the performance of such SQL statements?

Function should be used in poor performance.

Performance is not good, because you cannot use index writing to use the index (if any) select 'id' from 'table' where 'flag' like '1%'

You want to filter items. use your method. Have you ever wondered what to do if you want to add attributes to your previous data?

Performance is not good, because you cannot use index writing to use the index (if any) select 'id' from 'table' where 'flag' like '1%'

Is it to add an index to the flag field? is like more efficient than left?

You want to filter items. use your method. Have you ever wondered what to do if you want to add attributes to your previous data?
I am thinking, for example, the product has 10 Attributes. in my flag field, each attribute corresponds to a number. now I only use the first two attributes, and I automatically add 0 to the back, assign values later. Do you have any better classification methods?

Create an attribute table aid attribute
Gid of a commodity table
When both aid and gid in the corresponding edge of the aid gid table can index extended attributes, you only need to edit the attribute table.

As long as the left side of the like matching string is not a variable, the index can be used for 1, 2, and 11. Therefore, after, 2 like '1% ', will always be retrieved quickly, because they are together

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.