在同一个表中交叉引用2个字段

时间:2014-02-22 00:17:02

标签: php mysql sql codeigniter

我正在用PHP开发一个应用程序(实际上是codeigniter)+ MYSQL ...我会尽量把它简单化为

我正在我的产品表上做一个选择,所以我得到以下内容:

color        size        quantity
red          s           2
red          m           3
red          l           4
red          xl          1
blue         s           1
blue         m           0
blue         l           0
blue         xl          1

我需要以这种方式显示结果

color        s        m        l        xl
red          2        3        4        1
blue         1        0        0        1

任何想法??? SQL或PHP就可以了......

提前致谢。

4 个答案:

答案 0 :(得分:1)

不可否认,在看到其他帖子直接从数据库中进行分组后,这种方法看起来很蹩脚。但是,您可以使用PHP格式化数据,方法是将每种颜色存储在自己的数组中,然后从那里迭代它:

<?php

$link = mysqli_connect('localhost', 'user', 'pass', 'database');

$q = 'SELECT * FROM product ORDER BY color';
$result = mysqli_query($link, $q);

$color_array = array();

while ($row = mysqli_fetch_array($result)) {

    $color = $row['color'];
    $size = $row['size'];
    $quantity = $row['quantity'];

    $color_array[$color][$size] = $quantity;

}

foreach ($color_array AS $color_key => $color_item) {

    print $color_key."\t".$color_item['s']."\t".$color_item['m']."\t".$color_item['l']."\t".$color_item['xl']."\n";

}

编辑:

我在评论中看到您可能有1到6个尺码。我的原始代码不会真的有用,所以我修改了一下来从数组中读取大小,然后在数据库中找到它们时将它们打印出来。

<?php

$link = mysqli_connect('localhost', 'user', 'pass', 'database');

$size_array = array('s', 'm', 'l', 'xl', 'xxl', 'xxxl');

$q = 'SELECT * FROM colors ORDER BY color';
$result = mysqli_query($link, $q);

$color_array = array();

while ($row = mysqli_fetch_array($result)) {

    $color = $row['color'];
    $size = $row['size'];
    $quantity = $row['quantity'];

    $color_array[$color][$size] = $quantity;

}

foreach ($color_array AS $color_key => $color_item) {


    print "\n".$color_key."\t";

    foreach ($size_array AS $size) {
        print isset($color_item[$size]) ? $color_item[$size]."\t" : "\t";
    }

}

答案 1 :(得分:0)

如果您可以使用SELECT子句中的大小进行硬编码,那么您可以使用以下查询:

SELECT color,
       SUM(IF(size = 's', quantity, 0)) s,
       SUM(IF(size = 'm', quantity, 0)) m,
       SUM(IF(size = 'l', quantity, 0)) l,
       SUM(IF(size = 'xl', quantity, 0)) xl
FROM products
GROUP BY color

否则,您可以使用循环(例如foreach)在PHP中实现它。

答案 2 :(得分:0)

在sql中尝试此解决方案

<强>更新:
1.使用更多示例数据进行测试
2.更改光标的SQL查询以处理每个大小的总数量。

示例数据:(此数据位于临时表@temp中)

color                size  quantity
-------------------- ----- -----------
red                  s     2
red                  m     3
red                  l     4
red                  xl    1
Yellow               s     5
blue                 s     1
blue                 m     0
blue                 l     0
blue                 xl    1
red                  xl    1
blue                 xl    1
Green                xl    1

SQL查询:

declare @temp2 table
(
  color nvarchar(20),
  s int,
  m int,
  l int,
  xl int
)


declare @color nvarchar(20), @size nvarchar(5), @quantity int

declare myCursor Cursor for
select color,size,sum(quantity) from @temp group by color,size order by color, size

Open myCursor
Fetch next from myCursor into @color, @size, @quantity

while @@FETCH_STATUS = 0
begin



        if @size = 's'  
         begin
            update @temp2 set s = @quantity where color = @color
            if @@ROWCOUNT = 0
             insert into @temp2 (color, s) values (@color, @quantity)
         end
        if @size = 'm'
         begin  
            update @temp2 set m = @quantity where color = @color
            if @@ROWCOUNT = 0
                insert into @temp2 (color, m) values (@color, @quantity)
         end
        if @size = 'l'
         begin  
            update @temp2 set l = @quantity where color = @color
            if @@ROWCOUNT = 0
                insert into @temp2 (color, l) values (@color, @quantity)
         end
        if @size = 'xl' 
         begin 
            update @temp2 set xl = @quantity where color = @color
            if @@ROWCOUNT = 0
             insert into @temp2 (color, xl) values (@color, @quantity)
         end




    Fetch next from myCursor into @color, @size, @quantity
end

close myCursor
Deallocate myCursor

select color, isnull(s,0) as s, isnull(m,0) as m, isnull(l,0) as l,isnull(xl,0) as xl from @temp2 

结果数据:

color                s           m           l           xl
-------------------- ----------- ----------- ----------- -----------
blue                 1           0           0           2
Green                0           0           0           1
red                  2           3           4           2
Yellow               5           0           0           0

答案 3 :(得分:0)

所以,感谢@Razvan,我这样做了......

    $query_size=$this->db->query("SELECT size FROM sizes inner join product on sizes.product_type=product.type where product.id='$product_id'");
    $sql="SELECT color";
    foreach($query_size->result() as $size){
        $sql=$sql . ", SUM(IF(size = '$size->size', quantity, 0)) as $size->size";
    }
    $sql=$sql . " FROM products WHERE id='$product_id' GROUP BY color";
    $query=$this->db->query($sql);