如何使用匹配的字符串过滤sql查询结果

时间:2015-11-21 09:20:30

标签: php mysql json

我有一个数据库表,其中一列包含字符串值,看起来像php中的字典。

表:学院

Id  name    requirement
1   x   {"users": ["A", "B"], "name": "*", "pool": "CSE", "place": "Bangalore"}
2   y   {"users": ["A", "C"], "name": "*", "pool": "CSE", "place": "Chennai"}
3   z   {"users": ["A", "B"], "name": "*", "pool": "ECE", "place": "Bangalore"}
4   r   {"users": ["A", "D"], "name": "*", "pool": "EEE", "place": "UP"}

从这个表中我只需要过滤那些需求列包含“pool”:“CSE”的行。 也就是说,如果池是CSE或池是ECE等。

目前我获取整个表的php代码是:

    $query = "SELECT * FROM waitqueue";
    $result = mysql_query($query);

    echo "[";
    echo json_encode(mysql_fetch_assoc($result));
    while ($row = mysql_fetch_assoc($result))
        echo "," . json_encode($row);
    echo "]";

我不想更改上面的sql查询。因为我需要单个查询中的两个条件(完整表和选定的池行)。所以我希望从结果中解析。

如何匹配"池"以及如何过滤这些行?

任何人请建议我。

提前致谢。

1 个答案:

答案 0 :(得分:1)

在您的情况下,您可以像这样过滤

java.sql.SQLRecoverableException: I/O-Fehler: 
The Network Adapter could not establish the connection
at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:478)
at oracle.jdbc.driver.PhysicalConnection.<init>(PhysicalConnection.java:547)
at oracle.jdbc.driver.T4CConnection.<init>(T4CConnection.java:225)
at oracle.jdbc.driver.T4CDriverExtension.getConnection(T4CDriverExtension.java:29)
at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:556)
at oracle.eclipse.tools.database.connectivity.db.OracleJDBCConnection.createConn(OracleJDBCConnection.java:193)
at oracle.eclipse.tools.database.connectivity.db.OracleJDBCConnection.createConnection(OracleJDBCConnection.java:149)
at org.eclipse.datatools.connectivity.DriverConnectionBase.internalCreateConnection(DriverConnectionBase.java:105)
at org.eclipse.datatools.connectivity.DriverConnectionBase.open(DriverConnectionBase.java:54)
at org.eclipse.datatools.connectivity.drivers.jdbc.JDBCConnection.open(JDBCConnection.java:96)
at oracle.eclipse.tools.database.connectivity.db.OracleJDBCConnectionFactory.createConnection(OracleJDBCConnectionFactory.java:26)
at org.eclipse.datatools.connectivity.internal.ConnectionFactoryProvider.createConnection(ConnectionFactoryProvider.java:83)
at org.eclipse.datatools.connectivity.internal.ConnectionProfile.createConnection(ConnectionProfile.java:359)
at org.eclipse.datatools.connectivity.ui.PingJob.createTestConnection(PingJob.java:76)
at org.eclipse.datatools.connectivity.ui.PingJob.run(PingJob.java:59)
at org.eclipse.core.internal.jobs.Worker.run(Worker.java:54)
Caused by: oracle.net.ns.NetException: The Network Adapter could not establish the connection
at oracle.net.nt.ConnStrategy.execute(ConnStrategy.java:372)
at oracle.net.resolver.AddrResolution.resolveAndExecute(AddrResolution.java:419)
at oracle.net.ns.NSProtocol.establishConnection(NSProtocol.java:873)
at oracle.net.ns.NSProtocol.connect(NSProtocol.java:258)
at oracle.jdbc.driver.T4CConnection.connect(T4CConnection.java:1577)
at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:353)
... 15 more
Caused by: java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at oracle.net.nt.TcpNTAdapter.connect(TcpNTAdapter.java:206)
at oracle.net.nt.ConnOption.connect(ConnOption.java:120)
at oracle.net.nt.ConnStrategy.execute(ConnStrategy.java:350)
... 20 more

但这是非常大的拐杖:)

更喜欢将列要求分隔为列:名称,池,位置。 比你可以像这样过滤,例如:

WHERE requirement LIKE '%"pool": "CSE"%'

其他方法是像这样过滤你的while循环中的行

WHERE pool = "CSE"