SQL Query to search partial data in one field - mysql

I have the following data in mysql:
file-id
32-534
32-536
32-537
32-584
32-594
46-865
46-863
46-837
46-867
I want to write SQL query to enlist all the data with either 32- or 46-. How can I do that?
Select * from TABLE WHERE file-id = ????

This should work:
SELECT * FROM TABLE WHERE 'file-id' LIKE '32-%' OR 'file-id' LIKE '46-%';

SELECT * FROM table_name WHERE file-id REGEXP '^32' OR file-id REGEXP '^46';

Try:
SELECT * FROM table_name WHERE file-id REGEXP '^(32|46)';

Related

write a query that applies to an entire db instead of a table

Is it possible to write a query that applies to an entire database as opposed to one table.
So instead of usin:
select * from table_name where columnName = ?
Can I say select * db_tables from from db where the table contains the column A?
Is is possible?
thanks
You sure can!
SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE COLUMN_NAME like '%A'
You cannot do SELECT * FROM all the tables, but you can run a query aganist multiple tables using other statements in SQL.

how to query this statement ( where name like '%job's you%') in mysql

I am try select from mysql database with this
select * from mytable where where name like '%job's you%');
It's give me error message. How to fix it? thank for you help!
The ' in job's is escaping your query and the ) shouldn't be there. Try:
select * from mytable where where name like '%job\'s you%';
You have two time where , an improper ) and you have a single quote inside so you should wrap with double quotes
select * from mytable where name like "%job's you%";
The ' in "job's" is escaping your query, you can try:
select * from mytable where where name like "%job's you%"
Use real_escape_string.
$variable = $mysqli->real_escape_string("job's you");
$query = $mysqli->query("select * from mytable where where name like '%".$variable."%')");
Please follow the link for more details.

MySQL: Select multiple fields

If SQL query contains all fields what I need it will be very large.
How in SQL query I can select field from F3 to F 100 and from F150 to F200?
SELECT F3 to F100, F150 to F200 FROM database;
It is possible or not???
Tables structure change is not available
You have to :
1- manually select all columns . Or
2- do
Select * from database
And then just fetch the columns you need.
There are no shortcuts for this, you will have to list the fields needed one way or another. If the fields being selected are always the same, you should create a view for that as:
CREATE VIEW SomeView AS
SELECT
F3,
...
F100
FROM
SomeTable
and then select like:
SELECT * FROM SomeView
But again, you will have to list the fields at least once.
SELECT F3 to F100, F150 to F200 FROM database;
this query can not possible..
you must have to specify all the columns name
like select F1,f2,f3 from database;
You can't.
But if it's not possible to modify your table structure to fix the database design issue, you can use an SQL query to generate the MySQL query:
SELECT CONCAT('SELECT ', GROUP_CONCAT(COLUMN_NAME), ' FROM `your table`')
FROM information_schema.COLUMNS
WHERE TABLE_SCHEMA = 'your schema' and TABLE_NAME = 'your table'
GROUP BY TABLE_NAME
Add a filter in WHERE to select only the desired fields.

Select from MYSQL where the first four characters are

I have a database, in one of the fields occasionally I get an entry which starts off as mail.domainname.com
Is it possible using mysql and php to select only the rows from the field hostname where the first 4 characters = 'mail'?
One way to do it is to use LIKE
SELECT * FROM tablename WHERE hostname LIKE 'mail%'
Another is to use SUBSTR()
SELECT * FROM tablename WHERE SUBSTR(hostname, 1, 4) ='mail'
Case sensitive:
SELECT * FROM tbl WHERE hostname LIKE BINARY 'mail%'
Case insensitive:
SELECT * FROM tbl WHERE hostname LIKE 'mail%'
Yes, you can use SQL LIKE operator: http://www.w3schools.com/sql/sql_like.asp
Simply use:
SELECT * FROM your_table WHERE SUBSTR(field_name, 1, 4) = 'mail'

MySQL: Split string SQL to be used in WHERE clause

I am looking for a way to use the IN keyword in JasperReport. My query looks like:
SELECT * FROM table_name WHERE CID IN (145,45, 452);
following that in jasper report I can setup this;
SELECT * FROM table_name WHERE CID IN ($P{MY_CIDS});
and from my Java I would send $P{MY_CIDS} as a String, so my query will look like
SELECT * FROM table_name WHERE CID IN ("145,45, 452");
My questions is how to transform in SQL "145,45, 452" to a valid query so it would take into consideration each value separately 145, 45, 452
All help is appreciated.
WHERE FIND_IN_SET(CID, "145,45,452")
But this query will always cause table fullscan. So I suggest you to rewrite your code and use proper IN (A, B, C) syntax.
You've two options. The more common, is to rewrite your query like this instead:
SELECT * FROM table_name WHERE CID IN (?, ?, ?, ..., ?);
I'm not sure the other is valid for MySQL, but it works fine in PostgreSQL. It is to use an immutable function that parses the string into a set of integers, and instead use:
SELECT * FROM table_name WHERE CID IN (split_to_int(?));
SELECT * FROM table_name WHERE CID IN ($P!{MY_CIDS});
Just use
$P!{MY_CIDS}
(exclamation mark between $P and {MY_CIDS}).
Jasper will now concatinate the Strings like this:
"SELECT * FROM table_name WHERE CID IN ("
+"145,45,452"
+")";
resulting in:
SELECT * FROM table_name WHERE CID IN (145,45,452);
Took me a long time to get that but now it works for me."