Cheking multiple columns in a table with single select query - php

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)

Related

SHOW COLUMNS FROM table for several selected Fields

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';

sql for find out a value from json_encided column in mysql?

I have a column of json_encoded in my table and want to check a value exist or not in that column. What type of query should write?
Example: I have a column in which values are: ["1","2","3","4"] and I want to check through query that 1 exists or not.
Sql Query :
SELECT * FROM `tableName` WHERE JSON_CONTAINS(fieldName, '["valueTobeChecked"]');
Assumptions: The data type of field should be "json"
Try:
SELECT * FROM `tableName` WHERE Column LIKE '%"1"%';
Use JSON_CONTAINS() function accepts the JSON document being searched and another to compare against. It returns 1 when a match is found,
SELECT * FROM `book`
WHERE JSON_CONTAINS(Field, '["values"]');

I have comma seperated column and need to get most common values from them in mysql

I have comma separated value in mysql table field and want to get most common value amongst them.
i.e. I have a table name is A, In which there are two fields id and available_values.
id available_values
--- -----------------
1 3,5,7,9
2 3,5
3 5,9
In above example there are value 5 exist in all rows so i need to get this value(mean - 5), because this is available in every records.
Please help to find out the solution.
Thanks in advance.
Best way is to include another table with one row per id and your values. But you can use REGEXP to achieve the output :
SELECT * FROM table_name WHERE
available_values REGEXP CONCAT('(^|,)(', replace(5,',','|'),')(,|$)')
SELECT * FROM `test` WHERE FIND_IN_SET($keysValue,available_values)
example
SELECT * FROM `tableName` WHERE FIND_IN_SET($keysValue,columnsName)

How to check with a value in comma separated values in MySQL?

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);

Display only the columns that contains data

I have a database that have 35 columns and some of those columns are empty and I've already made the page and the query to retrieve the data from the data base but the problem is All the data including the empty ones, and I want to know if there is a resolving to these problem.
you can use the where clause in the query
like
Select * from TableName where FieldName is Not Null And Field2 is not null
Similarly you can add as many filters as you want
Use the WHERE clause:
WHERE somefield IS NOT NULL
Your query (assuming you're using SQL) should have something like
... my_table.my_field IS NOT NULL ...
see also: MySQL select where column is not empty

Categories