Example: I want sql to output only rows that have 6 digit of character 'AAAAAA' I don't want rows that have more than that.
"SELECT * FROM table WHERE LENGTH(columnName) = 6" If MySql and
"SELECT * FROM table WHERE LEN(columnName) = 6" If MSSql
SELECT * FROM table WHERE field REGEXP '^[A]{6}$';
Related
ID tags
--------------------------------
1 1,2,3,4
2 2,3,4
3 4,22
4 2
this is my MySQL query
"SELECT * FROM table_name WHERE tags LIKE '%2%'"
when I write above query to search only '2' in tags column it will give me '22' as a result. So how do I prevent get '22' when I search only '2'. I need to get result as
ID tags
--------------------------------
1 1,2,3,4
2 2,3,4
4 2
use FIND_IN_SET(str,strlist)
SELECT * FROM table_name WHERE FIND_IN_SET(2, tags)
Try this MySQL query,
"SELECT * FROM table_name WHERE FIND_IN_SET(2, tags)"
It will help you
I have a SQL Select query in PHP where i need to lookup rows in a table where a column is like a string with and without spaces
for example, if my value in the database is ABC 123 and i search for ABC123 the query will be
$sql = SELECT * from table where col_name LIKE '%ABC123%' ";
but i need it to return the row where col_name equals ABC 123
and vice versa, for example, if the database value is 123ABC and i search for 123 ABC the query will be
$sql = SELECT * from table where col_name LIKE '%123 ABC%' ";
and i need it to return the row where col_name equals 123ABC
$sql = SELECT * from table where REPLACE(col_name,' ','') LIKE '%ABC123%' ";
This is my table:
ID NAME GROUP
123456 Example 1
789012 Test 2
345678 Lorem 1
This code works fine:
select * from mytable where id="789012"
However, this code fails:
select * from mytable where group="1"
Why is this? Isn't the whole point of iterating with the while loop to return multiple rows?
Your query fails. You need to escape reserved words in MySQL like group with backticks
select * from mytable where `group` = 1
I would like to select multiple values in where clause but it is not selecting anything.
This is the select query I have:
'SELECT * FROM table WHERE id IN (4, 5) ORDER BY id desc'
what am I missing?
Based on your comments, the reason that the query is failing is because the column is a varchar and you are using int values in your IN clause. MySQL does not convert the type if you quote the numbers then your query will work with varchar
http://dev.mysql.com/doc/refman/5.0/en/type-conversion.html
I would imagine your table has no data with id = 4 or id = 5.
Try
SELECT * FROM table WHERE id = 4
Does that return anything either? I would bet no.
Why not just 'SELECT * FROM table WHERE (id = '4' OR id= '5') ORDER BY id desc'
this data is getting from some emp table table:--->"php,codeigniter,seo,html,javascript,techsupport,html,hr,finance"
here my problem based on the above data
the following table if any string matches skill from above data then show that id
table:
id skill
---------------------
1 php
2 php,hr
3 javascript,html,seo
4 sap
5 oracleapps
the result should only display 1,2,3 rows only it is possible to get that data?
I am using this query but it is fetch exact match records only
select * from seekerdetails jsd
where find_in_set( jsd.key_skills,( select lower( GROUP_CONCAT(key_skills))
from
empjobs))
First, explode your $table list, and then add it one-by-one to the FIND_IN_SET call:
$t = explode(",",$table);
$q = "SELECT * FROM seekerdetails WHERE ".
"FIND_IN_SET(".implode("', key_skills) OR FIND_IN_SET('", $t).", key_skills)";