1

I have an attribute in MYSQL table "primaryCamera", which has value as

Camera 8 MP, 3264 x 2448 pixels, Carl Zeiss optics, optical image stabilization, autofocus, dual

I want to list mobiles greater than or equal to 8MP, what is exact query in MYSQL which works for me.

Please help me.

6
  • I was looking for "LIKE", but this is not good. Any regular expression needed. Commented Jun 12, 2016 at 6:53
  • 1
    Camera 8 MP or Camera 9 MP is what you want to fetch and Camera 7 MP is not. Does this column all like this format Camera X MP? Commented Jun 12, 2016 at 6:57
  • This column has various numbers, such as 9MP, 3MP , 12MP. I need a query, which is used from less than or greater than a number. Such as i Have query. SELECT primaryCamera FROM table_name WHERE primaryCamera REGEXP '[8]' It work fine. But there are also some other integer in column. As mentioned above. What can i do Commented Jun 12, 2016 at 7:04
  • 1
    You didn't get it. Are all your data under primaryCamera column in this format Camera X MP..........................................? @razaulmustafa Commented Jun 12, 2016 at 7:19
  • Yes. I have data, as you said. Please give me query now Commented Jun 12, 2016 at 7:23

2 Answers 2

1

If the data under primaryCamera column are in the following format then the query given below might do the job.

Format: Camera X MP.....

Query:

SELECT 
primaryCamera
FROM table_name 
WHERE 
CAST(SUBSTRING(@str FROM LOCATE('Camera',@str)+LENGTH('Camera') FOR (LOCATE('MP',@str)-(LOCATE('Camera',@str)+LENGTH('Camera')))) AS UNSIGNED) > 8;

Explanation:

  • LOCATE('Camera',@str) returns the start index of the string Camera.
  • LENGTH('Camera') returns the length of the string Camera which is 6.
  • (LOCATE('MP',@str) returns the start index of the string MP.

Example:

Given,

primaryCamera = 'Camera 8 MP, 3264 x 2448 pixels, Carl Zeiss optics, optical image stabilization, autofocus, dual'

startIndex of string Camera = 1

Length of Camera = 6

start Index of string MP = 10.

The integer value you are looking for lies within this range [7,10].

SUBSTRING(string FROM START_INDEX_OF_DESIRED_SUBSTRING FOR LENGTH_YOU_WANT_TO_EXTRACT) works like this.`

In order to get that you need to do this:

START_INDEX_OF_DESIRED_SUBSTRING = StartIndex of Camera + Length of Camera

LENGTH_YOU_WANT_TO_EXTRACT = Start position of stringMP- START_INDEX_OF_DESIRED_SUBSTRING

SUBSTRING(primaryCamera FROM START_INDEX_OF_DESIRED_SUBSTRING FOR LENGTH_YOU_WANT_TO_EXTRACT );

Note: You should store the attributes of your product (e.g. Camera) in different columns under different table. Otherwise you are soon going to embrace lot of cumbersome tasks to process even for the simplest task.

Sign up to request clarification or add additional context in comments.

8 Comments

This is not working. Can you please re-write this to this format. 8MP. I don't have "Camera" in the beginning
Show me one real data under primaryCamera column.
SELECT primary FROM specifications_new_vastgsm WHERE LOCATE('MP',primary) and primary>=8.....This work for me
which one @Reno? (:p) I am asking for one real data / the question posted here by the PO?
@1000111 Sorry, I mean the question itself.:( Author did not get what you said and can not make this question more clearly.
|
0

use sub string, the syntax is :

SELECT SUBSTR('string',start,length)

replace string here with your query

1 Comment

Please give me query. I have attribute PrimaryCamera and i want to select mobiles which have camera greater than 8MP.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.