简体   繁体   English

codeigniter mysql sql 查询

[英]codeigniter mysql sql query

Hello everyone which MySQL query that can output with my example and how i can echo.大家好,哪个 MySQL 查询可以用我的示例输出以及我如何回显。 i need help here's example..我需要帮助这里的例子..

                'table 1
                | id |date        |
                | 1  |01/01/2015  |
                | 2  |01/02/2015  |
                | 3  |01/01/2015  |
                | 4  |01/02/2015  | 

                'table 2
                |id |table1_id| value1| value2| 
                | 1 |   1     |  5    |    2  |
                | 2 |   2     |  40   |    3  |
                | 3 |   3     |   5   |    2  |
                | 4 |   4     |       |    4  | 

output must输出必须

          $value_total = value1 * value2;

          if(value1 == ''){

          }else{
           $value_total = value1 * value2;
           }

Here's The final output.这是最终的输出。 will add with the same date将添加相同的日期

         date           value
         01/01/2015     20
         01/02/2015     120

Try this:尝试这个:

if(!empty(value1){
    $value_total = value1 * value2;
}
SELECT t1.date, (t2.value1*t2.value2) As V FROM `table2` as t2
INNER JOIN table1 as t1
On t1.id=t2.table1_id
GROUP by t1.date

You can get the expected output by the following query.Please try this and post your comments.您可以通过以下查询获得预期的输出。请尝试此操作并发表您的意见。

SELECT 
    `t1`.`date`, 
    (`t2`.`value1`*`t2`.`value2`) as value 
FROM
    `table2` as t2
JOIN 
    `table1` as t1 
ON
    `t1`.`id` = `t2`.`table1_id` 
GROUP BY
    `date` 

Or:或者:

SELECT * FROM `table1` as t1 JOIN `table2` as t2 ON `t1`.`id` = 't2'.`table1_id` GROUP BY `date`

and in your php get the value1 and value 2 values and check in if condition like并在您的 php 中获取 value1 和 value 2 值并检查条件是否如下

if (!empty($value1)) {
    echo $totalValue = $value1 * $value2;
}

Mysql query in Codeigniter style Codeigniter 风格的 Mysql 查询

$this->db->select('date, sum(t2.value1 * t2.value2) AS value', FALSE)->from('table1 t1');
$this->db->join("table2 t2", "t1.id=t2.table1_id", "LEFT");
$this->db->group_by('date');
$result = $this->db->get();

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM