Php 语法帮助以及向查询添加表和条件

Php 语法帮助以及向查询添加表和条件,php,sql,Php,Sql,假设上面的插入是dkb的三个项目,请参见在查询dbl.id=dkb.id中它在dkb.id上连接的dbl.id Well dbl.id对于dkb表中的一个项目有三个价格,在本例中是项目1的三个价格、项目136的三个价格和项目2的三个价格。因此,它有三种不同的种类,并对dkb中的每一项进行了描述。从现在起,如果您查看一下查询下面while循环中分配的字段,其中一些是dkb字段,另一些是cdkb和dbl字段。正如我所说,从dkb表的信息来看,while循环中的设置是可以的,因为dkb表只有一个价格和

假设上面的插入是dkb的三个项目,请参见在查询dbl.id=dkb.id中它在dkb.id上连接的dbl.id Well dbl.id对于dkb表中的一个项目有三个价格,在本例中是项目1的三个价格、项目136的三个价格和项目2的三个价格。因此,它有三种不同的种类,并对dkb中的每一项进行了描述。从现在起,如果您查看一下查询下面while循环中分配的字段,其中一些是dkb字段,另一些是cdkb和dbl字段。正如我所说,从dkb表的信息来看,while循环中的设置是可以的,因为dkb表只有一个价格和一个名称。我想的是安排一个if语句条件来检测信息来自page1.php的位置,这是一个名称和价格,或者来自page2.php的位置,这将是一个名称,从一个到三个价格和种类,取决于用户选择的小型、中型或大型托盘。每个托盘都有一个价格,但它将是一个名称1、136或2,这些是来自dkb表的id。在while循环中的if语句中,显示page2.php中需要一到三种价格和品种的信息将有一个foreach循环,或者可能有一个for循环,但不知道用户在page2.php中选择了多少品种,以便在购物车中的while循环中显示这些信息,并使用if语句检测天气从page1.php或page2.php中选择

INSERT INTO `dbl` (`id`, `price`, `variety`, `description`) VALUES
(1, 20.30, 'Small Tray', 'Serves 6 to 8 people' ),
(1, 25.90, 'Medium Tray', 'Serves 12 to 15 people'),
(1, 30.90, 'Large Tray', 'Serves 18 to 21 people'),
(136, 0.00, 'Small Tray', '', 'small'),
(136, 0.00, 'Medium Tray', '' ),
(136, 0.00, 'Large Tray', ''),
(2, 0.00, 'Small Tray', ''),
(2, 0.00, 'Medium Tray', ''),
(2, 0.00, 'Large Tray', '');

请给我最后一手资料。

如果您单独选择了所有选项,然后选择表格。*这可能是您的问题

 while($row = mysql_fetch_array($result))
{  
if (table cdkb) { 
  [1]qyt         image      name          price        remove
     1            ---       marina        $18.90        remove?    
     }end of if statement
Else table dkb {
   1]qyt            Image         Name             Price                  Remove
     1               ---         marina         Small Tray  $18.90        remove? 
                                                Medium Tray $30.24
                                                Large Tray  $35.90
                  } // end of else statement

                  }//end of while loop

要为此MySQL查询设置一些数据,请执行以下操作:

SELECT *
FROM
cart

LEFT OUTER JOIN dkb
   ON cart.id = dkb.id

LEFT OUTER JOIN cdkb
   on cart.id = cdkb.id

LEFT OUTER JOIN double
   on cart.id = double.id
WHERE 
cart.cookieId = GetCartId() AND cdkb.id IS NOT NULL
然后,此查询的结果:

-- **********************************************************************
-- Table holding all the items in the users shopping cart, along with
-- a cookieId for thier session
-- **********************************************************************
Create table cart
   (
   cartId           integer,
   cookieId         integer,
   id               integer,
   qty              integer
   )
go

-- **********************************************************************
-- Table all the items available for sale with the price
-- **********************************************************************
Create table dkb
   (
   id               integer,
   name             varchar(20),
   price            decimal(12,2)
   )
go

-- **********************************************************************
-- Table that looks like the items table, but we have no idea why it is
-- here.
-- **********************************************************************
Create table cdkb
   (
   id               integer,
   name             varchar(20),
   price            decimal(12,2)
   )
go

-- **********************************************************************
-- Another table with a price in it .. a completely different id field
-- and a variety ... WTH is it for - No idea.
-- **********************************************************************
Create table dblv
   (
   dbl_id           integer,
   price            decimal(12,2),
   variety          varchar(20)
   )
go

insert into cart (cartid, cookieid, id, qty) values (1, 1, 1, 1);
go
insert into cart (cartid, cookieid, id, qty) values (1, 1, 2, 1);
go
insert into cart (cartid, cookieid, id, qty) values (1, 1, 3, 1);
go
insert into cart (cartid, cookieid, id, qty) values (1, 1, 4, 1);
go
insert into cart (cartid, cookieid, id, qty) values (1, 1, 5, 1);
go
insert into cart (cartid, cookieid, id, qty) values (1, 1, 6, 1);
go
insert into cart (cartid, cookieid, id, qty) values (1, 1, 7, 1);
go
insert into cart (cartid, cookieid, id, qty) values (1, 1, 8, 1);
go

insert into dkb (id, name, price) values (1,'my dkb 1', 10.00);
go
insert into dkb (id, name, price) values (2,'my dkb 2', 20.00);
go
insert into dkb (id, name, price) values (3,'my dkb 3', 30.00);
go
insert into dkb (id, name, price) values (4,'my dkb 4', 40.00);
go

insert into cdkb (id, name, price) values (5,'my cdkb 5', 50.00);
go
insert into cdkb (id, name, price) values (6,'my cdkb 6', 60.00);
go
insert into cdkb (id, name, price) values (7,'my cdkb 7', 70.00);
go
insert into cdkb (id, name, price) values (8,'my cdkb 8', 80.00);
go

insert into dblv (dbl_id, price, variety) values (1,1.99,'my dbl 1 variety');
go
insert into dblv (dbl_id, price, variety) values (2,1.99,'my dbl 2 variety');
go
结果如下:

SELECT
   cart.id    cart_id,
   dkb.id     dkb_id,
   cdkb.id    cdkb_id,
   cart.*,
   dkb.*,
   cdkb.*,
   dblv.*
FROM
    cart

    LEFT OUTER JOIN dkb
       ON cart.id = dkb.id

    LEFT OUTER JOIN dblv
       on dkb.id = dblv.dbl_id

    LEFT OUTER JOIN cdkb
       on cart.id = cdkb.id

抱歉,今天时间不多-工作时间很长。:-)我明天会回来查看..

我想在购物车上显示的第一个外部连接dkb.id=dkb.id,其中dkb.id=$idc,以及要在购物车上显示的第二个外部连接cdkb.id=dkb.id,其中dkb.id=$id$idc来自page1.php到cart.php的url,$id来自page2.php到cart.php的url。。。。cart.id=dkb.id,其中dkb.id=$id;cart.id=cdkb.id,其中cdkb.id=$idc,其中cart.cookieId=GetCartId()因此,我认为$idc应该位于cart.id=$idc上,而不是dkb.id,在这种情况下,IS NOT NULL的意思是什么?在我发布表结构之前,我想在发布时添加双表连接到dlb.id=dkb.id上,并将其表信息与dkb表一起显示。它们都需要打开,因为它们同时显示信息。请看我编辑的第一篇文章,我已经用字段名发布了完整的查询。正是因为您希望它仅在cart.id=dkb.id时显示。。。因此,它确实加入了dkb.id.:-)您的最后一条评论是指我在您的回复帖子后的最后一条评论或我编辑的第一条帖子?是指上面的第一条评论。:-)无论如何,看来你的问题解决了!
-- **********************************************************************
-- Table holding all the items in the users shopping cart, along with
-- a cookieId for thier session
-- **********************************************************************
Create table cart
   (
   cartId           integer,
   cookieId         integer,
   id               integer,
   qty              integer
   )
go

-- **********************************************************************
-- Table all the items available for sale with the price
-- **********************************************************************
Create table dkb
   (
   id               integer,
   name             varchar(20),
   price            decimal(12,2)
   )
go

-- **********************************************************************
-- Table that looks like the items table, but we have no idea why it is
-- here.
-- **********************************************************************
Create table cdkb
   (
   id               integer,
   name             varchar(20),
   price            decimal(12,2)
   )
go

-- **********************************************************************
-- Another table with a price in it .. a completely different id field
-- and a variety ... WTH is it for - No idea.
-- **********************************************************************
Create table dblv
   (
   dbl_id           integer,
   price            decimal(12,2),
   variety          varchar(20)
   )
go

insert into cart (cartid, cookieid, id, qty) values (1, 1, 1, 1);
go
insert into cart (cartid, cookieid, id, qty) values (1, 1, 2, 1);
go
insert into cart (cartid, cookieid, id, qty) values (1, 1, 3, 1);
go
insert into cart (cartid, cookieid, id, qty) values (1, 1, 4, 1);
go
insert into cart (cartid, cookieid, id, qty) values (1, 1, 5, 1);
go
insert into cart (cartid, cookieid, id, qty) values (1, 1, 6, 1);
go
insert into cart (cartid, cookieid, id, qty) values (1, 1, 7, 1);
go
insert into cart (cartid, cookieid, id, qty) values (1, 1, 8, 1);
go

insert into dkb (id, name, price) values (1,'my dkb 1', 10.00);
go
insert into dkb (id, name, price) values (2,'my dkb 2', 20.00);
go
insert into dkb (id, name, price) values (3,'my dkb 3', 30.00);
go
insert into dkb (id, name, price) values (4,'my dkb 4', 40.00);
go

insert into cdkb (id, name, price) values (5,'my cdkb 5', 50.00);
go
insert into cdkb (id, name, price) values (6,'my cdkb 6', 60.00);
go
insert into cdkb (id, name, price) values (7,'my cdkb 7', 70.00);
go
insert into cdkb (id, name, price) values (8,'my cdkb 8', 80.00);
go

insert into dblv (dbl_id, price, variety) values (1,1.99,'my dbl 1 variety');
go
insert into dblv (dbl_id, price, variety) values (2,1.99,'my dbl 2 variety');
go
SELECT
   cart.id    cart_id,
   dkb.id     dkb_id,
   cdkb.id    cdkb_id,
   cart.*,
   dkb.*,
   cdkb.*,
   dblv.*
FROM
    cart

    LEFT OUTER JOIN dkb
       ON cart.id = dkb.id

    LEFT OUTER JOIN dblv
       on dkb.id = dblv.dbl_id

    LEFT OUTER JOIN cdkb
       on cart.id = cdkb.id
cart_id    dkb_id     cdkb_id    cartId     cookieId   id         qty        id         name                 price        id         name                 price        dbl_id     price        variety              
---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- -------------------- ------------ ---------- -------------------- ------------ ---------- ------------ -------------------- 
1          1          NULL       1          1          1          1          1          my dkb 1             10.00        NULL       NULL                 NULL         1          1.99         my dbl 1 variety     
2          2          NULL       1          1          2          1          2          my dkb 2             20.00        NULL       NULL                 NULL         2          1.99         my dbl 2 variety     
3          3          NULL       1          1          3          1          3          my dkb 3             30.00        NULL       NULL                 NULL         NULL       NULL         NULL                 
4          4          NULL       1          1          4          1          4          my dkb 4             40.00        NULL       NULL                 NULL         NULL       NULL         NULL                 
5          NULL       5          1          1          5          1          NULL       NULL                 NULL         5          my cdkb 5            50.00        NULL       NULL         NULL                 
6          NULL       6          1          1          6          1          NULL       NULL                 NULL         6          my cdkb 6            60.00        NULL       NULL         NULL                 
7          NULL       7          1          1          7          1          NULL       NULL                 NULL         7          my cdkb 7            70.00        NULL       NULL         NULL                 
8          NULL       8          1          1          8          1          NULL       NULL                 NULL         8          my cdkb 8            80.00        NULL       NULL         NULL