mysql - 从2个表中获取数据(MySQL)

标签 mysql

我有两个具有相同列的表,我需要显示两个表中的所有列,但现在我得到了每行中的所有列,例如。每行定位器 2 次,它必须仅显示正确表中的 1 个。

SELECT a.*, b.* FROM  clothes a, items b group by a.locator,b.locator

我该怎么做?

我将从两个带有行的表中输出它。 “名称”、“定位器”、“价格”和 WHERE“ibutik”= 1。

带有测试行的衣服表:

   CREATE TABLE IF NOT EXISTS `clothes` (
   `id` int(11) NOT NULL AUTO_INCREMENT,
   `locator` varchar(48) DEFAULT NULL,
   `name` varchar(32) DEFAULT NULL,
     `price` int(11) DEFAULT '100',
    `level` smallint(6) DEFAULT '0',
     `type` smallint(6) DEFAULT NULL,
     `sex` smallint(4) DEFAULT NULL,
     `x_offset` smallint(6) DEFAULT '0',
     `y_offset` smallint(6) DEFAULT '0',
     `nontradeable` tinyint(4) DEFAULT '0',
     `ibutik` int(1) NOT NULL,
     `koebt` int(9) NOT NULL,
     PRIMARY KEY (`id`)
   ) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=55 ;


 INSERT INTO `clothes` (`id`, `locator`, `name`, `price`, `level`, `type`, `sex`, `x_offset`, `y_offset`, `nontradeable`, `ibutik`, `koebt`) VALUES
   (1, '1.png', 'Male body', 100, 1, 2, 1, 0, 0, 0, 0, 0),
   (3, '1.png', 'Female body\r\n', 100, 1, 1, 2, 0, 0, 0, 0, 0))

和项目:

CREATE TABLE IF NOT EXISTS `items` (
      `id` int(11) NOT NULL AUTO_INCREMENT,
  `locator` varchar(32) DEFAULT '',
  `name` varchar(32) DEFAULT '',
  `price` int(11) DEFAULT '100',
  `level` smallint(6) DEFAULT '0',
  `rotateable` tinyint(4) DEFAULT '0',
  `x_offset` smallint(6) DEFAULT '0',
  `y_offset` smallint(6) DEFAULT '0',
  `z_index` smallint(6) DEFAULT '0',
  `nontradeable` tinyint(4) DEFAULT '0',
  `ibutik` int(1) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=20 ;



INSERT INTO `items` (`id`, `locator`, `name`, `price`, `level`, `rotateable`, `x_offset`, `y_offset`, `z_index`, `nontradeable`, `ibutik`) VALUES
(1, 'rodplante.png', 'Rød plante', 10, 0, 0, 0, 0, 0, 0, 1),
(2, '1.png', 'Gul plante', 0, 0, 0, 0, 0, 0, 0, 0),
(3, '2.png', 'Gul plante', 0, 0, 0, 0, 0, 0, 0, 0),

最佳答案

可以尝试这样的事情:

select id,locator,name,price, tableName from 
(
  select a.id as id, a.locator as locator, a.name as name, 
     a.price as price, a.ibutik as ibutik, 'closes' as tableName 
    from  clothes a
  union all
  select b.id,b.locator,b.name,b.price,b.ibutik,'items' from items b 
) foo
where ibutik=0;

已通过 sqlFiddle 进行验证.

关于mysql - 从2个表中获取数据(MySQL),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13207676/

相关文章:

java - Hibernate 相当于这个 MySQL 查询的是什么?

php - Mysql查询在wamp服务器mysql控制台中运行,但不通过python文件或控制台

php - 每个 php 都有多个 post 值

mysql - mysql如何向多个表中插入数据

php - 从 SQL 查询中提取行数并将其作为变量放入 PHP 中?

php - 登录功能似乎无法正常运行

MySQL - 多个点之间的SUBSTRING_INDEX

php - 计算最高每小时费率?

连接行的MySQL顺序?

php - Laravel:如果不确定是否需要该子句,如何附加到查询