大家好我在从表中选择一些数据然后根据表1中的字段值将表连接到另一个表时遇到一个简单的问题
就像我有 table1 , table2 和表3 。 我想
<g:form controller="animais">
<g:hiddenField name="rootNode" value="${rootNode}"/>
<g:hiddenField name="finalAnswer" value="${finalAnswer}"/>
<h3>${curQuestion} </h3>
<g:if test="${finished}">
<g:actionSubmit class="btn btn-primary" action="backToStart" value="Voltar" />
</g:if>
<g:if test="${!finished}">
<g:if test="${!showDivTip}">
<g:textField name="finalAnswer" value="${finalAnswer}"/>
<g:actionSubmit class="btn btn-primary" action="submitFinalAnswer" value="OK" />
</g:if>
<g:if test="${showDivTip}">
<g:textField name="tipToFinalAnswer" value="${tipText}"/>
<g:actionSubmit class="btn btn-primary" action="submitTipForAnswer" value="Finalizar" />
</g:if>
</g:if>
</g:form>
。
我现在可以这样做,但我不是那么可以用它来帮助我解决问题..
答案 0 :(得分:1)
我认为可以使用UNION
调用来实现:
SELECT t2.f1, t2.f2, t2.f3
FROM table2 t2
INNER JOIN table1 t1
ON t1.f1 = t2.f1
WHERE t2.f3 = 1
UNION ALL
SELECT t3.f1, t3.f2, t3.f3
FROM table3 t3
INNER JOIN table1 t1
ON t1.f1 = t3.f1
答案 1 :(得分:0)
试试这个:
SELECT t1.field1 AS t1field1,
t1.field2 AS t1field2,
CASE WHEN t1.field3 = 1 THEN t2.field1 ELSE t3.field1 END AS t2t3field1,
CASE WHEN t1.field3 = 1 THEN t2.field2 ELSE t3.field2 END AS t2t3field2,
CASE WHEN t1.field3 = 1 THEN t2.field3 ELSE t3.field3 END AS t2t3field3
FROM table1 t1
LEFT JOIN table2 t2 ON t1.field1 = t2.field1
LEFT JOIN table3 t3 ON t1.field1 = t3.field1;