简体   繁体   中英

Search data from multiple tables using sql query

In mysql Database I've following tables and html search form. Now I want to search selected data (From Search Form) from multiple tables. Selected data could be gender , budget or location . For location I've 7 tables. (No. 2-8) and each 7 tables second column value could be 0 OR 1 when user signup as there Location.So in this Location field user can select Central or East Or West or...others when searching.

So how do I write a sql query ( or Sql Join Query) to get the result from Mysql tables for search ?

Php Variable :

$gender = inputvalid($_POST['gender']);
$budget = inputvalid($_POST['budget']);
$location = inputvalid($_POST['location']);

Table name with column name

1. tutor_signup_form        (tutor_id, gender, feerange, name, email)
2. tutor_signup_pre_central (tutor_id, pre_c_central, place1, place2, place3)
3. tutor_signup_pre_west    (tutor_id, pre_w_west, place1, place2, place3)
4. tutor_signup_pre_east    (tutor_id, pre_e_east, place1, place2, place3)
5. tutor_signup_pre_south   (tutor_id, pre_s_south, place1, place2, place3)
6. tutor_signup_pre_north   (tutor_id, pre_n_north, place1, place2, place3)
7. tutor_signup_pre_ne      (tutor_id, pre_ne_northEast, place1, place2, place3)
8. tutor_signup_pre_nw      (tutor_id, pre_nw_northWest, place1, place2, place3)

and I've a search form is look like this:

<tr>
    <td style="width:100px;">1. Gender:</td>
    <td>    
        <select name="gender">
        <option value="">--Select--</option>
        <option value="male">Male only</option>
        <option value="female">Female only</option>
        </select>
    </td>   
    </tr>
<tr>
    <td>2. Budget:</td>
    <td>
        <select name="budget">
        <option value="" SELECTED>--Select--</option>
        <option value="90">$90 and below</option>
        <option value="80">$80 and below</option>
        <option value="70">$70 and below</option>
        <option value="60">$60 and below</option>
        <option value="50">$50 and below</option>
        <option value="40">$40 and below</option>
        <option value="30">$30 and below</option>
        <option value="25">$25 and below</option>
        <option value="20">$20 and below</option>
        <option value="15">$15 and below</option>
        <option value="10">$10 and below</option>
        </select>
    </td>
</tr>
<tr>
    <td>3. Location:</td>
    <td>
    <select name='location'>
    <option value="" SELECTED>--Select--.</option>
    <option value='1' />Central City</option>
    <option value='2' />East City</option>
    <option value='3' />West City   </option>
    <option value='4' />South City  </option>
    <option value='5' />North City  </option>
    <option value='6' />North East  </option>
    <option value='7' />North West  </option>       
    </select>
    </td>
</tr>

you can try this: EDITED

  if($location == '1'){
    $tableName="tutor_signup_pre_central";
    }
    if($location == '2'){
    $tableName="tutor_signup_pre_west";
    }

same for other table also..then sql.

$sql = "select tutor_signup_form.*,$tableName.place1,$tableName.place2,$tableName.place3 from  tutor_signup_form left join $tableName on tutor_signup_form.tutor_id = $tableName.tutor_id where tutor_signup_form.gender= $gender and tutor_signup_form.feerange=$budget"; 

can use some modifications if required.

It should be purpose for searching data and You must be use myisam engine.

Your database design is too normalization.(No. 2-8) and each 7 tables have same column set.

You can merge as single database like tutor_signup_form (tutor_id, gender, feerange, name, email,location, place1, place2, place3).

You write query like select * from tutor_signup_form where gender='' or budget ='' or location ='' . whatever condition need.

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