Regular expression in mysql which start with 1 - mysql

Hello i need query for select all records from database containig &var separted by semicolons.
where &var is any number bellow which start with 1, like 123,166,1444
my varchar column look like this "123;166;234;1444"
the column may look and like this 233;166;12, which also containg numbers which start with 1.

select * from table_name where column_name like '1%' or column_name like '%;1%'
replace table_name with your tablename . Also replace column_name with your column name

Related

how to select dynamic columns which are result of another query in mySQl

I have two tables.
First table have column called column_name which contains values columns names of the second table like ( column1 , column2 , etc).
I need to select columns from the second table depending on the result of query column_name from the first table .
I need help solving this issue but I cant develop it:
If You work with Oracle, i suggest You to use listagg function (http://www.oracle-developer.net/display.php?id=515)
And Query will be like:
Select column_name from all_tab_columns where table_name = name_of_your_table2 and column_name in (select listagg...)
if I understand correctly.

Like function which contains space as one of the character in mysql

how to use like function which contains space as one of the character in msql.
In my database, i am having column name as "contractor_id".
This is my code for selecting column name from table.
SELECT COLUMN_NAME as a FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name = '$table' AND column_name LIKE '%".actor id."%'.
Just leave out the quotation marks like
SELECT COLUMN_NAME as a
FROM INFORMATION_SCHEMA.COLUMNS
WHERE table_name = '$table'
AND column_name LIKE '%actor id%'
Or, in case you are generally looking for a column name with a blank in it use
... column_name like '% %'

how to know the count of words starting with a particular word in database?

I am using a mysql database for storing the data. The table consists of 2 columns s_no and name. I used index on this table on name. Now I want to get only words starting with a particular letter say "a" or "b" etc. How can I do it? Is there any SQL query for retrieving data in this fashion?
select s_no,name from tablename where name like 'a%' or name like 'b%'
select * from table_name
where name like 'a%'
select s_no,name from table where name like 'a%' or name like 'b%'
Select * from tablename where name like 'a%'
You can also pass the value inside parameters
#value varchar(50)
Select * from tablename where name like #value
so whatever characters u insert like a or b just mearge it with % and here you go
like #value=#value+ '%' before query .

Selecting records with underscore as second character in a column in MySQL using 'like'

I know for selecting the records with 'a' as the second letter in a column we write the following query:-
Select * from table where column1 like '_a%';
For selecting the records with 'a' as the third letter in a column we write:-
Select * from table where column1 like '__a%';
Now I want select the records where the column1 contains '_'(underscore) as the second character.For example, I want to select the records with column1 like A_John, B_John, A_Jai and so on. What is the escape character I can use in this case. How can I do that? Please help.
Use:
Select * from books where title like '\_\_a%';
for two underscore at first and
Select * from books where title like '\_a%';
for single underscore at first and
The '\' character is used to escape:
Select * from table where column1 like '_\_a%';
\ is the escape character in MySQL. Use:
Select * from table where column1 like '_\_%';

Selecting just one column instead of all columns in a mysql query

I am writing a PHP script where I will search for a keyword in a specific table and return the id of that keyword.
What I have done so far is:
SELECT * FROM 'table' WHERE column_name LIKE '%keyword%'
This returns the entire row of the keyword. What I want is a query that will just return the id of that row, so I can put it in a variable.
First, you should probably replace the quotes around 'table' with backticks, or you'll likely get syntax errors somewhere. The rest of my query examples use backticks for the table name. It's advised to use them for column names too to reduce the chance of conflicts.
Instead of using * to fetch all columns, explicitly list which columns you want to fetch:
SELECT id FROM `table` WHERE column_name LIKE '%keyword%'
The * wildcard selects all columns as you have noticed from your original query.
If you want to select other columns, list them as comma separated names:
SELECT id, foo, bar FROM `table` WHERE column_name LIKE '%keyword%'
Since you are using mySQL:
SELECT id FROM `table` WHERE column_name LIKE '%keyword%'