【问题标题】:Select all type of data从mysql中选择所有类型的数据
【发布时间】:2015-06-20 21:37:37
【问题描述】:

我的桌子是这样的:

id     name        seller    product
1      bags          s1       gold bag
2      shirt         s1       shirts
3      shows         s1       big show
4      jewellery     s1       jewellery
5      Tv            s2        Tv

我要选择 4 行,并且在 4 行中必须有卖家 s1s2 这两个结果。现在,当我选择 4 条记录时,前 4 条记录选择的是卖家 s1

编辑


我尝试过“GROUP BY”,但它只返回单个结果....我的意思是 id=1

和 从表中选择不同的卖家 只得到 2 行

我需要 "我要选择 4 行,并且在 4 行中必须有卖家 s1s2 这两个结果。"

【问题讨论】:

  • 我想成为第一个记录 id-1 和 2end - id-5 然后是其他人
  • 你有什么代码?
  • " 从表中选择不同的卖家" 不需要这个我需要选择 4 行并且在 4 行中必须有卖家 s1 和 s2 两个结果。现在,当我选择 4 条记录时,前 4 条记录选择的是卖家 s1

标签: mysql mysqli phpmyadmin


【解决方案1】:
select distinct seller from table 

虽然这只会返回 2 行

【讨论】:

  • 不需要这个我需要选择 4 行并且在 4 行中必须有卖家 s1 和 s2 两个结果。现在,当我选择 4 条记录时,前 4 条记录选择的是卖家 s1
  • 按原样查询不会产生 PO 请求的结果。
【解决方案2】:

以下解决方案将返回包含卖家 s1 和 s2 的 4 行。

代码段 1

代码段只为“s1”返回 3 条记录。这是通过将where seller = "s1"limit 3 子句添加到查询中来实现的。

select id, name, seller, product
from mytable
where seller = 's1'
limit 3

union 子句将两个代码段连接在一起。

代码段 2

第二个代码段执行与第一个代码段类似的子查询。添加了外部查询以确保限制子句只限于子查询,而不是整个结果。

select *
from (
select id, name, seller, product
from mytable
where seller = 's2'
limit 1) t1

这里是完整的代码,链接到SQL Fiddle demo

select id, name, seller, product
from mytable
where seller = 's1'
limit 3
union
select *
from (
select id, name, seller, product
from mytable
where seller = 's2'
limit 1) t1

【讨论】:

  • 嗨@Patricia,如果这个或任何答案解决了您的问题,请点击复选标记考虑accepting it。这向更广泛的社区表明您已经找到了解决方案,并为回答者和您自己提供了一些声誉。没有义务这样做。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-07
  • 2014-11-23
  • 1970-01-01
  • 2013-07-03
  • 2011-08-01
  • 2011-07-21
相关资源
最近更新 更多