简体   繁体   English

从表中选择,其中列名等于另一个表中的数据

[英]Select from table where columns names equal data in a different table

I am running this SQL Query: 我正在运行此SQL查询:

$sql2="SELECT * from callplanmeta ";
$rs2=mysql_query($sql2,$conn) or die(mysql_error());
while($result2=mysql_fetch_array($rs2))
{
    $column_list[] = $result2["callplanname"];
}

$sql="SELECT LEAST(NULLIF(".implode(',',$column_list).",0)) as num FROM callplandata ";
$rs=mysql_query($sql,$conn) or die(mysql_error());
while($result=mysql_fetch_array($rs))
{
    echo $result["description"].' - '.$result["num"].'<br><br>';
}

So in the callplanmeta table are rows with the column names for the callplandata table. 因此,在callplanmeta表中是带有callplandata表的列名称的行。

This query is working fine but I need a way to stop it from showing values that are zero from the $result["num"] result. 该查询工作正常,但我需要一种方法来阻止它显示来自$result["num"]结果为零的值。 I want it to not show the zero values and just show the next one for every row. 我希望它不显示零值,而只显示每行的下一个。

You should try: 你应该试试:

$sql="SELECT LEAST(NULLIF(".implode(',',$column_list).",0)) as num FROM callplandata WHERE LEAST(NULLIF(".implode(',',$column_list).",0)) != 0";

instead of: 代替:

$sql="SELECT LEAST(NULLIF(".implode(',',$column_list).",0)) as num FROM callplandata ";

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 SELECT 来自两个表 WHERE 每个表中的不同列等于 $id ORDER BY 公共列(PHP / MySQL) - SELECT from two tables WHERE different columns in each table equal $id ORDER BY common column (PHP/MySQL) 从多个表中选择列,其中每个表中的列等于一个变量 - Select columns from multiple tables where a column in each table is equal to a variable PHP / MySQL-尝试选择两列,其中table1中的id等于table2中的id时出现问题 - PHP/MySQL - Trouble when trying to select two columns where id from table1 is equal to id from table2 从表中选择行,该行等于选项symfony2 - Select from table where row equal to option symfony2 从MySQL中选择它不等于其他表中的行 - Select from mysql where it does not equal row in other table CodeIgniter Where_In从其他表中选择? - CodeIgniter Where_In select from different table? 如何 select 特定列并从这些列中获取数据并将它们存储在另一个表的不同列中? (拉拉维尔) - How to select specific columns and get data from those columns and store them in different columns in another table? (Laravel) 从一个表中选择多个列并将数据插入到 PHP-MySQL 中不同数据库中的另一个表中 - Select multiple columns from a table and insert data into another table in a different database in PHP-MySQL 从每个user_id不同的表中选择所有列 - Select all columns from a table where each user_id is different 通过在选择中切换选项,在不同的表列中显示不同的数据 - Displaying different data in different table columns by switching options in a select
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM