2014-11-25 25 views
0

我從表單字段獲取數據。它包含如何從mqsql表中選擇一半數據的數據

$city = "blr"; 
$field = "name"; 
$value= "mohai"; 


$sql = mysqli_query($con,"SELECT u.*, ad.*, ut.*, ci.*, st.* FROM user u, address ad, user_types ut, city ci, state st WHERE u.city_id=ci.id and ci.state_id=st.id and u.userId=ad.userId and 
    u.userType=ut.id and ci.city_name='".$city."' and u.".$field."='".$value."' and u.isDelete = '0'"); 

我正在使用上面的查詢獲取所有數據。 這裏我的問題是

用戶表中有name字段

對於前:數據在用戶表 - >「mohaideen」

如果給name字段MOHAI。它應該過濾來自表格的數據。

如何爲上述功能編寫查詢。

回答

0

要通過模式搜索,請在sql中使用%符號。 您的查詢將一些東西像下面

select from Table_name where some_property like modhi%; 
0

您可以使用LIKE在查詢中

  $sql = mysqli_query($con,"SELECT u.*, ad.*, ut.*, ci.*, st.* FROM user u, address ad, user_types ut, city ci, state st WHERE u.city_id=ci.id and ci.state_id=st.id and u.userId=ad.userId and 
u.userType=ut.id and ci.city_name='".$city."' and u.".$field." LIKE '%".$value."%' and u.isDelete = '0'"); 
0

匹配字符串匹配查詢通配符,你可以像使用條款

$sql = mysqli_query($con,"SELECT u.*, ad.*, ut.*, ci.*, st.* FROM user u, address ad, user_types ut, city ci, state st WHERE u.city_id=ci.id and ci.state_id=st.id and u.userId=ad.userId and 
u.userType=ut.id and ci.city_name='".$city."' and u.".$field." LIKE '%".$value."%' and u.isDelete = '0'"); 
0

爲此,您必須在您的查詢中使用LIKE關鍵字

這裏是一個很好的例子

請參閱本link

相關問題