【问题标题】:how to loop through multiple categories in sigle row using php and mysql如何使用php和mysql在单行中循环多个类别
【发布时间】:2016-07-15 11:07:05
【问题描述】:

我有一个数据库名称类别

 parent_cat      cat_id  title
    0             1      fruit
    0             2      vehicle
    0             3      goods
    1             4      sour
    1             5      sweet
    1             6      mixed
    2             7      sedan
    2             8      hatchback
    2             9      car   

我将一个对象存储在数据库表名产品中

obj_name   parent_cat   sub_id

  mango       1          4,5,
  maruti      2          7,8,9
  bmw         2          7,9

我想加入这两个表来显示数据,所以我需要在 URL 中传递参数,即。 ?obj=vehicle我通过sql查询得到的

SELECT category.cat_id,category.title,product.parent_cat,product.obj_name 
    FROM category, product 
    WHERE category.cat_id=product.parent_cat 
AND category.title='$title' --is a difined get variable

如果 title=fruit 我得到“mango” if title=vehicle 我得到 maruti 和 bmw 我想知道是title=sedan还是title=car,那么我怎样才能买到maruti和bmw 通过循环任何解决方案

【问题讨论】:

  • 不清楚你想做什么。
  • 到目前为止您尝试了什么?请添加您到现在为止尝试过的代码。
  • 我必须要根据类别标题显示 obj 名称的表格,因为类别标题有单个整数,因此根据类别标题显示 obj 名称很麻烦,但我如何通过循环sub_id 中的逗号分隔整数以显示 obj 名称,即。如果 URL 参数是 title=fruit 我得到了“mango” 如果 title=vehicle 我得到了 maruti 和 bmw 我想知道 title=sedan 还是 title=car 那么我如何通过循环 @Ullas 得到 maruti 和 bmw
  • title=fruit i got "mango" if title=vehicle i got maruti and bmw sql query is SELECT category.cat_id,category.title,product.parent_cat,product.obj_name FROM category, product WHERE category .cat_id=product.parent_cat AND category.title='$title' -- 是一个定义的 get 变量

    @prava

标签: php mysql arrays database multidimensional-array


【解决方案1】:

如果标题使用逗号分隔值IN(),您可能希望使用LEFT JOIN 查询

SELECT a.cat_id, a.title, b.parent_cat, b.obj_name 
FROM product b 
LEFT JOIN category a
ON a.cat_id = b.parent_cat 
WHERE a.title IN($title);

【讨论】:

  • 如果标题设置为 ?title=sedan 或 ?title=car,它会返回空结果,因为它是逗号分隔值 sub_id @Fabio
  • 检查更新的答案,如果您有逗号分隔值,请使用 IN()
  • 根据您的代码 if ?title=vehicle 它显示 maruti,BMW 因为它是字段中的单个 int 值但如果 ?title=sedan 它返回零值@Fabio
  • 这是因为你没有 id = 7 的 parent_cat
  • 我想用逗号分隔值保存一个与根和 obj 相关的父类别和多个子类别的 obj,所以如果我想从逗号分隔字段 @Fabio 中获取 id,我该如何实现跨度>
【解决方案2】:

试试这个:

SELECT category.cat_id, category.title, product.obj_name, product.parent_cat, product.sub_id FROM category
LEFT JOIN product ON category.cat_id = product.parent_cat OR category.cat_id LIKE '%product.sub_id%'
WHERE category.title LIKE '%$title%'

当您不确定要比较或查找的确切数据时,请使用 LIKE 而不是 =


[更新]

SELECT category.cat_id, category.title, product.obj_name, product.parent_cat, product.sub_id FROM product
LEFT JOIN category ON (product.parent_cat = category.cat_id OR product.sub_id LIKE '%category.cat_id%') AND category.title LIKE '%$title%'

我以错误的方向加入表格,对此深表歉意。

【讨论】:

  • 我得到 cat id title parent_cat sub_id 字段的值只有 9 ,车辆只有一个 1 结果,其余显示为 null @rh​​avendc
  • 我想要 9 辆 maruti 和 9 辆 bmw @rhavendc
  • @priyabratasen 哦,对不起。再次查看您的表格,我错误地加入了它们。请再试一次,我在上面提供了一个 [UPDATE] 查询。
  • 它显示了产品表中的所有数据我想知道如何处理逗号部分,我坚持他们。 @rhavendc
  • SELECT * FROM products WHERE LOCATE(CONCAT(',', 9 ,','),CONCAT(',',sub_id,',')) > 0; @rhavendc 现在可以了,我怎么加入
猜你喜欢
  • 1970-01-01
  • 2018-03-24
  • 1970-01-01
  • 1970-01-01
  • 2017-01-21
  • 1970-01-01
  • 2021-12-15
  • 2013-05-26
  • 1970-01-01
相关资源
最近更新 更多