【问题标题】:Searching String In All Tables And Displaying It With Pagination Technique在所有表中搜索字符串并使用分页技术显示
【发布时间】:2016-09-22 11:50:35
【问题描述】:

我的数据库有五个表:

Computer|Mobile|Tablet|MusicSystem|Camera 

所有表的结构都一样:

productID|ProductBrand|Price|userId

在这里,我想在所有表格中productBrand的所有字段中搜索产品的品牌名称,其中userId=$userId和limit是10,然后用分页技术显示。

如何在 MySQLi 中创建这样的查询并在 PHP 中显示?

提前致谢。

【问题讨论】:

  • a) 你必须在每个表中搜索,然后使用union 组合它们,最后使用limit 只选择一个页面 b) 你应该修复你的数据库设计,你的 5 个表属于1张桌子。这是谁设置的?这没有道理。 c) 如果你有这样一个正确的数据库设计,你只需要做一个select * from yournewtable where userId = $userId limit 10

标签: php mysql mysqli pagination


【解决方案1】:

如果您对每个类别表都遵循相同的结构,更好的结构是将所有分类到单个表中,如下所示

CREATE TABLE IF NOT EXISTS `category` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `category_name` varchar(255) NOT NULL DEFAULT '',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=6 ;

INSERT INTO `category` (`id`, `category_name`) VALUES
(1, 'Computer'),
(2, 'Mobile'),
(3, 'Tablet'),
(4, 'MusicSystem'),
(5, 'Camera');

CREATE TABLE IF NOT EXISTS `products` (
  `ProductId` int(11) NOT NULL AUTO_INCREMENT,
  `ProductBrand` varchar(255) NOT NULL,
  `Price` varchar(100) NOT NULL,
  `UserId` int(11) unsigned NOT NULL,
  `CatId` int(11) unsigned NOT NULL,
  PRIMARY KEY (`ProductId`),
  KEY `CatId` (`CatId`),
  CONSTRAINT `products_ibfk_1` FOREIGN KEY (`CatId`) REFERENCES `category` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=6 ;

INSERT INTO `products` (`ProductId`, `ProductBrand`, `Price`, `UserId`, `CatId`) VALUES
(1, 'Lenovo', '35000', 5, 3),
(2, 'Asus', '28350', 5, 2),
(3, 'Dell', '25000', 5, 3),
(4, 'MotoG', '12500', 5, 2),
(5, 'Samsung', '52000', 4, 1);

你可以使用

得到你的结果
SELECT * FROM products WHERE userId = '5'

查看FIDDLE

【讨论】:

    【解决方案2】:

    试试这个...

    从计算机 c、移动 m、平板电脑 t、音乐系统 ms、相机 cam 中选择 *,其中 userId = $userId 和(c.productBrand = searchProductBrand 或 m.productBrand = searchProductBrand 或 t.productBrand = searchProductBrand 或 ms.productBrand = searchProductBrand或 cam.productBrand = searchProductBrand ) 限制 10

    【讨论】:

      猜你喜欢
      • 2013-09-13
      • 2015-08-15
      • 1970-01-01
      • 2020-12-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多