MySQL Selecting Something with Array Information - mysql

Is there a way to do something like this?
SELECT * FROM tablename WHERE x CONTAINS "1"
Basically, I want to select data from the database where x contains a specific number. The thing is, the x column in any row could contain "1, 2, 3" and I want to select all those that contain 1, specifically 1, not 11 or anything that contains 1, but specifically a 1.
Here's an example:
id title x
-------------------
1 row1 1,22,3
2 row2 1,5
3 row3 5,91
4 row4 70
And I want my query to return rows 1 and 2. I don't want row 3, as the 1 is inside the number 91. I don't want row 4 because there's no 1 there either.

You can use the FIND_IN_SET function like so:
SELECT * FROM tablename WHERE FIND_IN_SET('1', x)
This will also get optimised to use bit arithmetic if you are calling it on a SET type.

You can try this:
SELECT * FROM tablename WHERE x REGEXP "(^|,)1(,|$)"

Ideally you'd normalize your 'x' column out to a separate table.
But... you could also hack it like this:
SELECT * FROM tablename WHERE x LIKE '%,1' OR x LIKE '1,%' OR x LIKE '%,1,%'
This basically just handles the three different cases where the "1" is the first, last or a middle element in your list. (note if you've got a space after your commas you'd change the last part to '%, 1,%'
EDIT: Actually Dmitriy's REGEXP is nicer, and a'r's FIND_IN_SET looks ideal.

Related

How do I make a google query that selects for row?

Example:
If my data set is
A B C
1 2 3
5 6 7
4 5 6
I could have "1", "5" and "4" show up by typing =query(A:C, select A).
I could have "1" and "4" show up by typing =query(A:C, select A where B < 6).
Lets say I wanted to query only entries that appeared after a certain row. In this case, row 3 is 4, 5, 6. So if I want only results that are row 3 or below, I could add a fourth column D somewhere, fill column D with =row(), and then have only **** show up by typing
=query(A:C, select A where D >= 3).
But I don't want to have to add a fourth column somewhere and fill it with the =row() formula. The query should be able to do this on its own.
Query parametres
try:
=QUERY(A:C,"select * offset 2",0)
offset parameter is zero base:
0 -- start from row 1
1 -- start from row 2
2 -- start from row 3
so on
You may find more usuful query tips here. Use special words: offset, limit, skipping. For example, to select only odd rows use:
=QUERY(A:C,"select * skipping 2",0)
Filter function
To have full control of rows you select, use this construction:
=filter(A:C,isodd(row(A:C))) -- only odd rows
=filter(A:C,row(A:C)=3) -- only 3-d row
=filter(A:C,row(A:C)>=3) -- all rows >= 3-d row
=query(filter(A:C,row(A:C)>=3),"select *") use filter + query
So, I had an issue like this, and here is what I did. I made a virtual column.
=query(A:C, select A where D >= 3) would then be something like
=query({A:C, ROW(A:C)}, "Select Col1 where Col4 >=3")
You have to make an array to query a column you do not want in your sheet. In doing so you can no longer use Column letters, so then A, B, C then becomes Col1, Col2, Col3 and so on.

Patern maching like Select Query

I have a table that contains this kind a structure of a column, how can I make select only from character 4 to 6 to ignore other character that are outside this boundary , I tried LIKE'%544%', RegExp. ect. ??
1 000544001
2 000054400
3 000544010
4 000344010
5 000544011
One way is to use substr():
where substr(col, 4, 3) = '544'
another is to use like:
where col like '___544%'
you can also use mid(col,start,length) statement
like this
select column1 from table1 where mid(column1,4,3);

MySQL query (condition)

I want to achieve one thing though I'm not sure if it's possible.
So let's say I have a table with few columns, but only two of them are of interest to me right now. Example of table:
Column 1 | Column 2
blabla | blablahhhhh
wor154 | blablahhhhh
word123 | word12435564
something | some4565
What I want to achieve, is to select all fields where first 5 or more symbols of value of Column 2 don't match with first 5 or more symbols of value of Column 1. So I don't want to select rows where 5 or more symbols of value of Column 1 match 5 or more symbols of value of Column 2. In example, query should return only 2nd and 4th rows
So, is it possible and if it's, how it can be achieved. Thank you.
I'd go with a SUBSTRING():
SELECT col1 FROM table WHERE SUBSTRING(col1, 1, 5) <> SUBSTRING(col2, 1, 5);
You can use something similar to this:
select *
from table1
where substring(column1, 1, 5) != substring(column2, 1, 5)
See SQL Fiddle with Demo

How to store and then search through CSV in sql?

I know that I can use IN(1,2,3) to select items from an SQL database where their value is either 1, 2 or 3, but what if I have a value in MYSQL which is a string, and is equal to '1,2,3' and I want to select that value. As far as I know I have to do this:
SELECT whatever FROM wherever WHERE sqlvariable = '1' OR sqlvariable LIKE '1,%' OR `sqlvariable` LIKE '%,1,%' OR `sqlvariable` LIKE '%,1'
which is fine if I want to simulate 'IN' sort of in reverse. But lets say I want to select sqlvariable where it contains either 1 or 2, then I have to do this:
SELECT whatever FROM wherever WHERE sqlvariable = '1' OR sqlvariable LIKE '1,%' OR `sqlvariable` LIKE '%,1,%' OR `sqlvariable` LIKE '%,1' OR sqlvariable = '2' OR sqlvariable LIKE '2,%' OR `sqlvariable` LIKE '%,2,%' OR `sqlvariable` LIKE '%,2'
Now lets say I use PHP to generate the above queries and I'm looking to find sqlvariable where it contains a 1 2 4 66 34 33 22 44 or 992. It's going to take an awfully long time for this query to run.
I could do a bitwise implementation, but then I'm limited to only having about 60 different items or so which isn't enough.
Is there a faster way to utilize a CSV query like the above in sql? I suppose it's sort of like IN() but in reverse.
You can simplify your queries using FIND_IN_SET:
SELECT whatever
FROM wherever
WHERE FIND_IN_SET('1', sqlvariable)
OR FIND_IN_SET('2', sqlvariable)
This however will still require searching every row in the table. It would be better to redesign your database so that CSV formatted data is not required.
Update: Your existing design looks something like this:
groupname groupmembers
Group1 1,2,3
Group2 3,4
You should change it to something more like this:
groupid groupmember
1 1
1 2
1 3
2 3
2 4

Having a number in

Can someone give me a query that will return as a result rows ID 1 & 3?
ID Name Hidden
1 Mika 1,4,2
2 Loca 0
3 Nosta 4
4 Like 2
Something like this
SELECT * FROM table WHERE Hidden HAVING(4)
SELECT * FROM table WHERE FIND_IN_SET('4',Hidden);
docs for FIND_IN_SET
SELECT * FROM table WHERE CONCAT(',',Hidden,',') LIKE '%,4,%'
or you can avoid using LIKE like this
SELECT * FROM table WHERE INSTR(CONCAT(',',Hidden,','), ',4,') > 0
this will not get things like 40, 14, etc, but you need to make sure there are no spaces in the Hidden field (eg, 1, 4, 5 or update the concat and LIKE function accordingly.
SELECT * FROM table WHERE Hidden LIKE '%4%'
the % are wildcards.
Full Text Search might be a reasonable solution for this as long as you use the correct word breaks.
Either go with Full Text Search, as suggested, or
Spin the Hidden values off into a separate table, with the ID of current row.
Eg, Mika would have three entries in this table
ID = 1, Hidden =1
ID = 1, Hidden =4
ID = 1, Hidden =2
Then you could return results against this spin off table.
You may also want to consider normalizing the table and storing these "hidden" values in a separate table with an index on the apropriate column. Depending on the number of rows you have that would be much faster:
ID Hidden
1 1
1 4
1 2
3 4
4 2
and:
SELECT DISTINCT table.* FROM table, hidden_table WHERE table.ID = hidden_table.ID AND hidden_table.hidden = 4