MYSQL:获取具有相似列的数据

时间:2015-06-11 14:08:17

标签: mysql

我的数据库中有这个:

id | column1
1 | ab
2 | cd
3 | cd
4 | ef
5 | cd
6 | ab

我想得到两个ID:(1,6)或(2,3,5)或(4)但它必须具有相同的column1名称

我没有要搜索的特定column1名称。

我想要的东西与:

相同
row=SELECT column1 FROM table LIMIT 1
row2=SELECT id FROM table WHERE column1=row[0].column1

但是在一个查询中,这可能吗?

2 个答案:

答案 0 :(得分:1)

您可以嵌套SQL查询

row= SELECT id FROM table WHERE column1=(SELECT column1 FROM table LIMIT 1)

答案 1 :(得分:0)

SELECT id  来自table 内部联接  table AS table2  ON table.column1 = table2.column1

可能是你最接近的,然后你只需使用WHERE子句,如 table.column1 =' ab'