SQL如何从不同的表中获取值?

时间:2017-08-17 00:15:10

标签: java mysql sql

我有两张桌子:

连接

_id  NAME   SERVER_IP   SERVER_PORT TOPIC   QOS
  1  home   192.168.1.102   1883    test     0
  2  home2  192.168.1.102   1800    test2    0

和在线:

_id  ONLINE
  1  false
  2  false

当我查询时

String rawQuery = "SELECT * FROM connection,online";
Cursor c = db.rawQuery(
           rawQuery,
           null
 );
dataModels = new ArrayList<>();
        while (c.moveToNext()) {
            String connection_name = c.getString(c.getColumnIndexOrThrow(DataBase.BaseEntry.CONNECTION_NAME));
            String online = c.getString(c.getColumnIndexOrThrow(DataBase.ServiceEntry.ONLINE));
            dataModels.add(new DataModel(connection_name, online));
        }

我得到乘法输出,如:home false,home2 false,home false,home2 false。我如何能够提出正确的请求并获得简单的输出,例如:home false,home2 false?

1 个答案:

答案 0 :(得分:0)

SELECT * FROM connection INNER JOIN online ON connection._id=online._id;

试图查询它,但问题是我使用的是_id=_id