简体   繁体   中英

How to select column where value equals?

I am trying to select the column where the value from a text input equals, i am being returned a value from the input and can echo it. But i cannot do a search and echo what column the value is in. i need to be able to echo the column name the value is in, and multiple if it is in more than one. Any help is appreciated.

 $skw = $_POST['k'];
    if(isset($_POST['k'])){
    $connect = mysql_connect('*', '*', '*')or die('Could not execute command. Please contact a administrator.');
    mysql_select_db('*')or die('No database');
    $query = "SELECT * FROM *TABLE*
    WHERE A1 LIKE '%$skw%' 
    OR B1 LIKE '%$skw%' 
    OR C1 LIKE '%$skw%' 
    OR D1 LIKE '%$skw%' 
    OR E1 LIKE '%$skw%' 
    OR F1 LIKE '%$skw%' 
    OR G1 LIKE '%$skw%' 
    OR H1 LIKE '%$skw%' 
    OR I1 LIKE '%$skw%' 
    OR J1 LIKE '%$skw%'
    OR A2 LIKE '%$skw%' 
    OR B2 LIKE '%$skw%' 
    OR C2 LIKE '%$skw%' 
    OR D2 LIKE '%$skw%' 
    OR E2 LIKE '%$skw%' 
    OR F2 LIKE '%$skw%' 
    OR G2 LIKE '%$skw%' 
    OR H2 LIKE '%$skw%' 
    OR I2 LIKE '%$skw%' 
    OR J2 LIKE '%$skw%'
    OR A3 LIKE '%$skw%' 
    OR B3 LIKE '%$skw%' 
    OR C3 LIKE '%$skw%' 
    OR D3 LIKE '%$skw%' 
    OR E3 LIKE '%$skw%' 
    OR F3 LIKE '%$skw%' 
    OR G3 LIKE '%$skw%' 
    OR H3 LIKE '%$skw%' 
    OR I3 LIKE '%$skw%' 
    OR J3 LIKE '%$skw%'
    OR A4 LIKE '%$skw%' 
    OR B4 LIKE '%$skw%' 
    OR C4 LIKE '%$skw%' 
    OR D4 LIKE '%$skw%' 
    OR E4 LIKE '%$skw%' 
    OR F4 LIKE '%$skw%' 
    OR G4 LIKE '%$skw%' 
    OR H4 LIKE '%$skw%' 
    OR I4 LIKE '%$skw%' 
    OR J4 LIKE '%$skw%'
    OR A5 LIKE '%$skw%' 
    OR B5 LIKE '%$skw%' 
    OR C5 LIKE '%$skw%' 
    OR D5 LIKE '%$skw%' 
    OR E5 LIKE '%$skw%' 
    OR F5 LIKE '%$skw%' 
    OR G5 LIKE '%$skw%' 
    OR H5 LIKE '%$skw%' 
    OR I5 LIKE '%$skw%' 
    OR J5 LIKE '%$skw%'
    OR A6 LIKE '%$skw%' 
    OR B6 LIKE '%$skw%' 
    OR C6 LIKE '%$skw%' 
    OR D6 LIKE '%$skw%' 
    OR E6 LIKE '%$skw%' 
    OR F6 LIKE '%$skw%' 
    OR G6 LIKE '%$skw%' 
    OR H6 LIKE '%$skw%' 
    OR I6 LIKE '%$skw%' 
    OR J6 LIKE '%$skw%'";
    $result = mysql_query($query)or die(mysql_error());
        while($data = mysql_fetch_row($result)){
     echo("<tr><td>$data[0]</td><td>$data[1]</td></tr>");
    }}

Consider normalizing your database so that all those fields A1 ... J6 are individual rows:

mytable_fields:
* field_ref (foreign key to mytable)
* field_nr
* field_value

Your query would then become:

SELECT field_nr, mytable.*
FROM mytable
INNER JOIN mytable_fields ON mytable.field_ref = mytable.id
WHERE field_value LIKE '%$skw%';

This is the only practical way you can tell which field contained the value you are searching for.

You should use show columns to get the column names, check the syntax below.

SHOW [FULL] COLUMNS {FROM | IN} tbl_name [{FROM | IN} db_name] [LIKE 'pattern' | WHERE expr]

Use a UNION , where each sub-query matches a single column, and outputs an additional column that says which column it's matching.

SELECT 'A1' AS Col, *
FROM Table
WHERE A1 LIKE '%$skw%'
UNION
SELECT 'B1' AS Col, *
FROM Table
WHERE B1 LIKE '%$skw%'
UNION
SELECT 'C1' AS Col, *
FROM Table
WHERE C1 LIKE '%$skw%'
...

If a row matches in multiple columns, it will turn into multiple rows in this result.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM