Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MYSQL query for searching through ALL the fields?

Tags:

mysql

As dumb as it sounds, is there a way to do something like this:

select row_id from mytable where * like '%searched_text%';

By * here I mean "all the fields" in the table, instead of me specifying them one by one...

like image 734
siliconpi Avatar asked Sep 26 '10 13:09

siliconpi


People also ask

How do I search all fields in a table?

Search for data in databases, schemes, and separate tablesRight-click the selection and select Tools | Full-Text Search. Alternatively, press Ctrl+Alt+Shift+F . In the search field of the Full-text Search dialog, type your search request and click Search.

How do I search an entire database in MySQL?

Find data across a MySQL connection by using the text search feature on any number of tables and schemas. From the schema tree, select the tables, schemas, or both to search and then right-click the highlighted items and click Search Data Table from the context menu.

How do I search for multiple columns in SQL?

To select multiple columns from a table, simply separate the column names with commas! For example, this query selects two columns, name and birthdate , from the people table: SELECT name, birthdate FROM people; Sometimes, you may want to select all columns from a table.


2 Answers

It is not possible with one query.

However when you do:

DESCRIBE table_name;

you get the field names which you can generate the query from.

Search in all fields from every table of a MySQL database May be useful.

like image 167
Wikeno Avatar answered Nov 01 '22 05:11

Wikeno


Here's a solution combined with some PHP to search all fields in a specific table.

include("db_con.php");
//search all fields
$searchphrase = "banan";
$table = "apa303";
$sql_search = "select * from ".$table." where ";
$sql_search_fields = Array();
$sql = "SHOW COLUMNS FROM ".$table;
$rs = mysql_query($sql);
    while($r = mysql_fetch_array($rs)){
        $colum = $r[0];
        $sql_search_fields[] = $colum." like('%".$searchphrase."%')";
    }

$sql_search .= implode(" OR ", $sql_search_fields);
$rs2 = mysql_query($sql_search);
$out = mysql_num_rows($rs2)."\n";
echo "Number of search hits in $table " . $out;
like image 41
K. Kilian Lindberg Avatar answered Nov 01 '22 03:11

K. Kilian Lindberg