I have a MySQL problem. This is just an example just to solve my real problem. Let's say I have a table like this:
Columns: id, name, classes.
Classes field is comma separated value like 1,4,5,4
I know database design is wrong and very bad.
What I want to do is select name where class is equal to 4 whereas the classes field has value 3,4,6,3,8 in MySQL. How can I do that?
You could use the FIND_IN_SET function in MySQL
SELECT name
FROM table_name
WHERE FIND_IN_SET('4',classes) > 0
use of find_in_set function like below-
select * from table_name where find_in_set('4',classes);
Related
I have a MySQL table where I am using comma separated values like:
user_name books
abc PHP,Java
xyz Net,Shift,PHP
I can handle comma-separated value searching using the FIND_IN_SET function of MySQL.
Now books names are auto suggested means if any one search with keyword "J" , system should search and match the word Java and give result of user "abc".
I tried to use like clause with FIND_IN_SET but it's not working.
Does anyone have any suggestions?
Not a clean solution but you can use something like this:
SELECT * FROM `library` WHERE `books` LIKE 'J%' OR `books` LIKE '%,J%'
How do I Show columns for specific fields .
SHOW COLUMNS FROM core_banking_mpesa WHERE FIELD= 'id' , FIELD ='LineNo' , FIELD ='Comments'
Your error is that WHERE column1='val', column2='val' is not valid syntax.
You could use IN() to select the fields:
SHOW COLUMNS FROM core_banking_mpesa
WHERE FIELD IN('id','LineNo','Comments')
Or use OR:
SHOW COLUMNS FROM core_banking_mpesa
WHERE FIELD='id' OR FIELD='LineNo' OR FIELD='Comments'
You could try just doing a SELECT query instead, requesting those three columns:
SELECT id, LineNo, Comments
FROM core_banking_mpesa;
If you already know what the columns are, it doesn't make much sense why you would want to use SHOW COLUMNS on this table.
First of all, in the database world, COLUMN and FIELD mean the same thing. So you can't "show columns for specific fields".
However if I understand correctly, you are trying to display data from a MySQL table for a specific set of columns/fields. If that is the case, you can try this. Use SELECT query this way :
select column_name
from information_schema.columns c
where table_name = 'core_banking_mpesa';
How to select data from two columns in same table in a single query.
for example select name "waseem" from username if not found check in city row.
select * from <table_name> where name like 'waseem' or city like 'waseem';
I agree with other's answers but with slight modification. If there is a record waseem ahemed and your only looking for waseem wont be searched in other answered query.
You need to use the wildcard % if you want to search for waseem.
I highly suggest you not to use * for selecting the column names because of the performance issues.
SELECT name, username, password WHERE name LIKE '%waseem%' OR city LIKE '%waseem%'
This is working for me
SELECT * FROM table WHERE row_name1 LIKE 'value' OR row_name2 LIKE 'value'
I'm passing a value to the database(Say-'gupta'). I want to compare this value with all the
columns present in the database table. If any column value matched the particular value then
I want to simply print that row in my Php Page.
Is there any inbuilt function to do this using select query.If yes please tell me.
Remember i want to compare the passed value('gupta') with the multiple columns values in the database(corephp+mysql)
USE LIKE along with OR. eg.
Select * from table where (col1 like '%gupta%') or (col2 like '%gupta%')
so any col matches your input word, this will return the row.
Just an idea
select * from TableName where "gupta" IN (ColumnOne,ColumnTwo,ColumnThree)
I have a db with 8 tables which intend to search for an id number by joining them but i would like to retrieve the name of the table to use in a php along the lines of...
if($tablename == 'accounts'){
echo 'value found in accounts';
}
What would the mysql query be?
(I don't need help with the joining etc, just the bit that would get the table name and how to store it in $tablename)
Thanks
SELECT table_name FROM INFORMATION_SCHEMA.TABLES
WHERE table_schema = 'db_name'
19.12. The INFORMATION_SCHEMA TABLES Table
How can you search for data in a table if you don't know the table name in the first place?
So I suppose that you already know the table names, and don't need things like SHOW TABLES; that you have e.g. a WHERE of the form
WHERE (table1.field LIKE '%search%' OR table2.field2 LIKE '%search%' OR ...)
and once retrieved a row, you know it matches, but you don't know where the match did occur. And running eight straight queries instead of one JOINed isn't an option.
If this is the case, you'll have to supplement your SELECTed fields with a flag to tell you what you need:
SELECT ..., CASE
WHEN accounts.field1 LIKE '%search%' THEN 'accounts'
WHEN customers.name LIKE '%search%' THEN 'customers'
...
END AS wtf FROM <rest of your query>
Then in PHP when retrieving the row, you will just use the field:
print "Value found in {$row['wtf']}";
'SHOW TABLES' afair, is it still used on mysql? :)