从表中获取符合条件的多个记录

从表中获取符合条件的多个记录

我有两个名为OFFICE_GROUP和OFFICE_IPS的表,





我想得到像这样的4行的结果:


使用的查询是这样的:

SELECT (SELECT oip.IP as IP FROM OFFICE_IPS oip, OFFICE_GROUP og WHERE og.OFFICE_GROUP_ID = oip.OFFICE_GROUP_ID
         AND og.MODULES like '%1%' lIMIT 1) AS SEMINAR,
(SELECT oip.IP as IP FROM OFFICE_IPS oip, OFFICE_GROUP og WHERE og.OFFICE_GROUP_ID = oip.OFFICE_GROUP_ID
         AND og.MODULES like '%2%'  lIMIT 1)  AS FAQ,
(SELECT oip.IP as IP FROM OFFICE_IPS oip, OFFICE_GROUP og WHERE og.OFFICE_GROUP_ID = oip.OFFICE_GROUP_ID
         AND og.GRP_LIMIT like '%1%'  lIMIT 1)  AS DEV_MODE,
(SELECT oip.IP as IP FROM OFFICE_IPS oip, OFFICE_GROUP og WHERE og.OFFICE_GROUP_ID = oip.OFFICE_GROUP_ID
         AND og.GRP_LIMIT like '%2%'  lIMIT 1)  AS META_API,
(SELECT oip.IP as IP FROM OFFICE_IPS oip, OFFICE_GROUP og WHERE og.OFFICE_GROUP_ID = oip.OFFICE_GROUP_ID
         AND og.GRP_LIMIT like '%3%'  lIMIT 1)  AS TRADING_SIGNAL


但是如果我想获得多行怎么办,我的意思是如果子查询返回多行。

在上面的查询中,如果我删除了“ ... LIMIT 1”,则会收到类似
#1242-子查询返回的行数超过1

任何人,请帮助我。

最佳答案

您应该在顶级查询的FROM部分而不是SELECT中进行子查询。
尝试做像

SELECT seminar, faq, dev_mode, meta_api, trading_signal
FROM (subselect) as seminars_table, (subselect) as faq_table....
WHERE join_contition_1 and join_condition_1;


然后,子选择需要包含联接列。

关于mysql - 从表中获取符合条件的多个记录,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/18074435/

10-11 02:33