繁体   English   中英

在所有表中搜索字符串并使用分页技术显示它

[英]Searching String In All Tables And Displaying It With Pagination Technique

我的数据库有五个表:

Computer|Mobile|Tablet|MusicSystem|Camera 

所有表都具有相同的结构,例如:

productID|ProductBrand|Price|userId

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

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

提前致谢。

尝试这个...

从Computer c,Mobile m,Tablet t,MusicSystem ms,Camera cam中选择*,其中userId = $ userId和(c.productBrand = searchingProductBrand或m.productBrand = searchingProductBrand或t.productBrand = searchingProductBrand或ms.productBrand = searchingProductBrand或cam。 productBrand = searchingProductBrand)限制10

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

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'

检查

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM