11

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...

siliconpi
  • 8,105
  • 18
  • 69
  • 107

7 Answers7

8

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.

Community
  • 1
  • 1
Wikeno
  • 419
  • 2
  • 4
4

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;
K. Kilian Lindberg
  • 2,918
  • 23
  • 30
  • 1
    Should be noted that in the very likely case that you change the search phrase to user input (for example $searchphrase = $_GET['query']) then you need to use mysql_real_escape_string to prevent SQL injection. – Aron Dec 13 '14 at 10:41
2

Yes, it's called Full Text Search.

You can use MySQL's built-in Full Text Search, or use a separate product to do you text indexing such as Apache's Lucene (my personal favorite).

Pablo Santa Cruz
  • 176,835
  • 32
  • 241
  • 292
0

I was looking something like this to search in all the fields of a table. Though my table having less data so I opted 'Kilian Lindberg' answer and make the PDO function using his idea. In this function we can sent the 'search string' and 'Table name' in parameter and it will return the SQL string which we can use further as per our requirement. Thought in big tables it may slowdown the process.

function columnsTable($Search, $Table){
    include("PDO_conn_detail.php"); /* your PDO mysql connection file */
    $srchSQLstr = "SELECT * FROM $Table WHERE ";
    $tableFields = Array();

    $colSQL = $conn->prepare("SHOW COLUMNS FROM $Table");
    $colSQL->execute();
    while ($result_colSQL = $colSQL->fetch(PDO::FETCH_ASSOC)){
            $tableFields[] = $result_colSQL[Field]." LIKE ('%".$Search."%')";
        }

    $srchSQLstr .= implode(" OR ", $tableFields);
    return $srchSQLstr;
}
-1

Use like this

select row_id from mytable where '%searched_text%' IN ('column1','column2','column3','column4','column5');
-1

This solution make it possible by Ms Sql Server via Linked server

DECLARE @searchTxt VARCHAR(100) = 'searching text'

DECLARE curr CURSOR LOCAL FAST_FORWARD READ_ONLY FOR
select * from openquery (BITRIX, 'SELECT table_name, column_name FROM information_schema.columns where table_schema = ''bitrix_50'' and data_type = ''varchar'' and table_name not like ''b_mail%''')

DECLARE @tName VARCHAR(100), @cName VARCHAR(100), @sql NVARCHAR(4000), @cnt INT

OPEN curr

FETCH NEXT FROM curr INTO @tName, @cName

WHILE (@@FETCH_STATUS=0)
BEGIN

    SET @sql = 'select @cnt=cnt from openquery (BITRIX, ''SELECT count(*) cnt FROM ' + @tName + ' WHERE ' + @cName + ' LIKE ''''%' + @searchTxt + '%'''''')'

    BEGIN TRY

        EXECUTE sp_executesql @sql, N'@cnt int OUTPUT', @cnt=@cnt OUTPUT

        IF (@cnt>0)
            PRINT @sql

    END TRY
    BEGIN CATCH
        PRINT N'Error on: ' + @sql
    END CATCH

    FETCH NEXT FROM curr INTO @tName, @cName

END

CLOSE curr
DEALLOCATE curr

Access MySQL data from SQL Server via a Linked Server

Partizan
  • 1
  • 1
  • 1
-2

He is a complete solution,,is the modification of the above solution,It worked for me, Thank you.

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title></title>
    </head>
    <body>
        <?php
        include('config.php');
        $searchphrase = "adsa";
        $table = "tenders";
        $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."<br />";
        while ($results = mysql_fetch_array($rs2)){
            print $results[0]."<br />";
        }
        ?>
    </body>
</html>
Thato Pebane
  • 134
  • 1
  • 4