简体   繁体   English

MYSQL SELECT从表的所有行中仅选择一列

[英]MYSQL SELECT only one column from all rows in table

I need to select all values from all rows by one column in my table. 我需要从表中的每一列中选择所有行中的所有值。 I have 我有

ID       TAGS
1        girls, womens, cars
2        girls, world, flowers
3        weapons, cars, boys

so after SQL I should get an array: 所以在SQL之后,我应该得到一个数组:

array('girls', 'womens', 'cars', 'girls', 'world', 'flowers', 'weapons', 'cars', 'boys')

so how my sql should look like? 所以我的sql应该是什么样子?

Never, never, never store multiple values in one column. 永不永不将多个值存储在一列中。

Like you see now this will only give you headaches. 就像您现在看到的那样,这只会让您头痛。 Normalize your table. 标准化表格。

USER_ID  TAG
1        girls
1        women
1        cars
2        girls
...

After that you could select your desired result like this 之后,您可以像这样选择所需的结果

select group_concat(distinct tag)
from your_table

Use GROUP_CONCAT() . 使用GROUP_CONCAT() A similar answer is already here MySQL DISTINCT on a GROUP_CONCAT() . MySQL DISTINCT在GROUP_CONCAT()上已经有一个类似的答案。

I can atleast give a sample solution: 我可以至少提供一个示例解决方案:

  1. use Cursor or something to traverse through each row of data 使用游标或其他东西遍历每一行数据
  2. split the data (there are couple of split example found in internet) sample example, Split string in SQL 拆分数据(在互联网上有几个拆分示例) 示例示例,S​​QL中的拆分字符串
  3. put the value in a temp table 将值放在临时表中
  4. finally select distinct from the table 最后选择与表不同

someone can put these example and make a solution. 有人可以举这些例子并提出解决方案。

暂无
暂无

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

相关问题 mysql从表中全选,但只更改一个列名 - mysql select all from table but change column name of only one MySQL:如何 select 表中除最后一行之外的所有行 - MySQL: How to select all rows from a table EXCEPT the last one MySQL从左连接表中插入一列的所有行? - MySQL insert into ALL rows of one column from left joined table? MySQL:仅返回一个表中的行,其中另一个表的一列中的所有值都相同 - MySQL: Return only rows in one table where ALL values in one column of another table are the same MySQL仅在数据透视表中选择一些行,并在表中选择所有行 - MySQL Select only some rows in pivot table and all rows in table PHP / MySQL-选择表1中的列等于表2中的列的所有行 - PHP/MySQL - Select all the rows where column from table 1 is equal to column from table 2 MySQL / PHP-选择表1中的列等于表2中的列的所有行,并创建一个foreach循环 - MySQL/PHP - Select all the rows where column from table 1 is equal to column from table 2 and creating a foreach loop MySQL-只有一列中的值的行与另一列中的所有行匹配 - Mysql - Only rows that value from one column matches for all from other column 如何从一个表中获取所有列,而从另一张表中仅获取具有ID的一列? -MySQL - How to get all columns from one table and only one column from another table with ID ? - MySql 仅按特定行显示所有数据组:从具有column ='value'的列的表组中选择* - show all data only group by specific rows : Select * from table group by column having column = 'value'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM