[英]How do I use mysql column contents as index (key) to php array in mySql query?
I want to know how or if I can use the content of a column in mysql as a key (index) to a php array within the mysql query. 我想知道如何或是否可以使用mysql中的列内容作为mysql查询中php数组的键(索引)。
Example: 例:
Column name: period 列名:期间
Column contents: (one of) 'Year','Month','Fortnight','Week','Hour','Day' 列内容:“年”,“月”,“两周”,“周”,“小时”,“天”(以下之一)
My php array: 我的PHP数组:
$x = array(
'Year' => 1,
'Month' => 12,
'Fortnight' => 26,
'Week' => 52,
'Hour' => 1872,
'Day' => 365
);
Another variable $y can be any number (say < 1,000,000) 另一个变量$ y可以是任何数字(例如<1,000,000)
I want to write a WHERE (or HAVING) condition such as... 我想写一个WHERE(或HAVING)条件,例如...
WHERE '$x[`period`]' * '$y' > 12345
Eg when the contents of period = 'Month', $y should be multiplied by 12 before being tested to be > 12345 例如,当period的内容='Month'时,应将$ y乘以12后再测试> 12345
What is the best (most efficient) way to do this, other than (say) a long CASE conditional? 除了(例如)长条件CASE以外,什么是最好的(最有效的)方法?
Thank you 谢谢
i have write demo query 我写了演示查询
<?php
$x = array(
'Year' => 1,
'Month' => 12,
'Fortnight' => 26,
'Week' => 52,
'Hour' => 1872,
'Day' => 365
);
$y=10;
$sql="select * from table where ".$x['Month']*$y." > 12345" ;
echo $sql;
in $x[]
select your value to multiply 在
$x[]
选择要乘的值
You can use case
instead of php array: 您可以使用
case
而不是php array:
<?php
$y = 100;
$sql = "select * from tablename
where (case `period`
when 'Year' then 1
when 'Month' then 12
when 'Fortnight' then 26
when 'Week' then 52
when 'Hour' then 1872
when 'Day' then 365
end) * {$y} > 12345";
Try this: 尝试这个:
<?php
$y = 10;
$period = "Month";
$x = array(
'Year' => 1,
'Month' => 12,
'Fortnight' => 26,
'Week' => 52,
'Hour' => 1872,
'Day' => 365
);
$sql="select * from table where ".($x[$period] * $y)." > 12345" ;
echo $sql;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.