如何从Php修改MySQL查询,所以如果我选择了ALL,那么就不会有Where语句,如果我选择了一些用户登录,那么只显示这个用户记录?
我需要这样的例子如果我选择了<option value="All">ALL</option>
查询将没有WHERE语句,但是,例如,如果我选择了<option value="admin">Admin</option>
,则此查询中将有Where tsales_funnel.Darijuma_vaditajs = 'admin'
SQL语句:
$sql = "Select
tsales_funnel.ID As ID,
wp_users.display_name As Darijuma_vaditajs,
tcportal_starpnieks.Cp_Name As Starpnieks,
tcportal_stucture.Cp_Name As OWCA,
n_products.Product_Nos As Produkts,
tsales_funnel_mrecord.Deadline As Deadline,
tsales_funnel_clients.Reg_nr As Klienta_Regnr,
tfirmas_reg.name_in_quotes As Klients,
tsales_funnel_mrecord.ID As Prod_ID,
tsales_funnel.Statuss As Statuss
From
tsales_funnel Left Join
tsales_funnel_mrecord On tsales_funnel_mrecord.Funnel_ID = tsales_funnel.ID
Left Join
tcportal_starpnieks On tcportal_starpnieks.Cp_code = tsales_funnel.Starpnieks
Left Join
tcportal_stucture On tcportal_stucture.Cp_code = tsales_funnel.OWCA Left Join
tsales_funnel_clients On tsales_funnel_clients.Funnel_ID = tsales_funnel.ID
Left Join
tfirmas_reg On tfirmas_reg.regcode = tsales_funnel_clients.Reg_nr Left Join
n_products On tsales_funnel_mrecord.Product_type = n_products.Product_Code
Left Join
wp_users On tsales_funnel.Darijuma_vaditajs = wp_users.user_login ".$sqlselecteduser."
Order By
Deadline";
表格中的GET方法:
<?php
if(isset($_GET['selecteduser'])){
$selecteduser = esc_sql($_GET['selecteduser']);
} else {
$selecteduser = $_SESSION['selecteduser'];
}
if($selecteduser == 'all') {
$sqlselecteduser = '';
} else {
$sqlselecteduser = " Where tsales_funnel.Darijuma_vaditajs = $selecteduser ";
}
?>
表格发送请求到这个页面:
<form action="" method="GET" id="selectdo">
<table border="1">
<tr>
<td>
<label for="selector1">Darījuma vadītājs:</label>
</td>
<td>
<select id="selector1" name="selecteduser">
<option value="all">-ALL-</option>
<?php
$sql = "SELECT display_name, user_login FROM wp_users";
$results = $wpdb->get_results($sql); // return an object, not ARRAY_N
if ($results) {
foreach ($results as $row) {
echo "<option value = '".$row->user_login."'>".$row->display_name."</option>";
}}
echo "</select></br>";
?>
</td>
<td><input type="submit" value="ok" /></td>
</tr>
</table>
</form>
之后数据显示在表格中。
答案 0 :(得分:1)
我更喜欢这种方式
$sql = "Select
tsales_funnel.ID As ID,
wp_users.display_name As Darijuma_vaditajs,
tcportal_starpnieks.Cp_Name As Starpnieks,
tcportal_stucture.Cp_Name As OWCA,
n_products.Product_Nos As Produkts,
tsales_funnel_mrecord.Deadline As Deadline,
tsales_funnel_clients.Reg_nr As Klienta_Regnr,
tfirmas_reg.name_in_quotes As Klients,
tsales_funnel_mrecord.ID As Prod_ID,
tsales_funnel.Statuss As Statuss
From
tsales_funnel Left Join
tsales_funnel_mrecord On tsales_funnel_mrecord.Funnel_ID = tsales_funnel.ID
Left Join
tcportal_starpnieks On tcportal_starpnieks.Cp_code = tsales_funnel.Starpnieks
Left Join
tcportal_stucture On tcportal_stucture.Cp_code = tsales_funnel.OWCA Left Join
tsales_funnel_clients On tsales_funnel_clients.Funnel_ID = tsales_funnel.ID
Left Join
tfirmas_reg On tfirmas_reg.regcode = tsales_funnel_clients.Reg_nr Left Join
n_products On tsales_funnel_mrecord.Product_type = n_products.Product_Code
Left Join
wp_users On tsales_funnel.Darijuma_vaditajs = wp_users.user_login
WHERE
IF('".$selecteduser."' = 'all', 1=1, tsales_funnel.Darijuma_vaditajs = '".$selecteduser."')
Order By
Deadline";
答案 1 :(得分:0)
使您的查询与此类似,您可以轻松添加“WHERE”语句:
SELECT *
FROM my_table
WHERE 0 = 0
[IF CONDITION OPENED]
AND my_field = ...
[/IF CONDITION CLOSED]
这是一个示例,您必须将其修改为您选择的代码和语言。