dtgsl60240 2017-04-18 18:24
浏览 21
已采纳

MYSQL不会返回超过1个类别

I've tried to find an answer to this problem, but I can't.

I have a 3 table format mqsql database. I use 1 table to add all of the product information, CarpetInfo, 1 Table to list my categories, CarpetCategories, and 1 Table to add categories to the products, CarpetCategorySort.

My CarpetCategorySort table has 3 columns, Manufacturer, Style, CategoryID. Example would be Manufacturer = Aladdin, Style = Alma Mater, CategoryID = 14/ 15/ 18/ 19/ 20/ 21/ 67/

My CarpetCategories Table has 2 Columns CategoryID and Category. 2 Examples would be CategoryID = 14, Category = Commercial & CategoryID = 15, Category = Commercial Loop

I can only get the code to work when I type in Commercial into the $category variable below. The code will not work if I type Commercial Loop into the $category variable. It's like it will only pull in the first number 14 and all of the others are ignored. The pricing order and everything else works right, just not the CategoryID part.

Here is my code.

<?php $mill = "Aladdin"; $category = "Commercial Loop";
$order = mysqli_query($con, "
SELECT * FROM CarpetInfo JOIN CarpetCategorySort USING (Manufacturer, Style) 
JOIN CarpetCategories USING (CategoryID)
WHERE Manufacturer='$mill' AND Category LIKE '%$category%'
order by Price = 0, Price asc,
Style asc");
include($_SERVER['DOCUMENT_ROOT'].'/includes/pricing/carpet-order-test.htm');?>

Any help is greatly appreciated!

  • 写回答

1条回答 默认 最新

  • doutuobao9736 2017-04-18 19:06
    关注

    That's a backward way of handling things, a field with several datapoints smooshed together like that really never has any justification for existence. I'd list the carpets in one table, list the categories in another, and finally list cross-references of both in another table, where you get a many-to-one relationship at both ends.

    You need an actual category table only if you think it's going to take so much room to give duplicate info for each category and/or you can't control user input for categories (like you aren't just using a pulldown to give a name choice).

    Something like:

    CREATE TABLE IF NOT EXISTS `carpets` (
      `id` int(11) NOT NULL,
      `name` varchar(50) NOT NULL,
      `abbrev` varchar(10),
      `description` varchar(250),
      [...]
      `updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
    ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=latin1;
    
    
    CREATE TABLE IF NOT EXISTS `carpet_categories` (
    `id` int(11) NOT NULL,
      `carpet_id` int(11) NOT NULL,
      `category_id` int(11) NOT NULL,
    [...]
    ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=latin1;
    
    
    CREATE TABLE IF NOT EXISTS `carpet_category_info` (
      `id` int(11) NOT NULL,
      `price-per-sqf` int(11) NOT NULL,
      `name` varchar(50),
       [...]
    ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=latin1;
    

    then all your joins become simple, and fast.. And accurate.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用