这是我从表order和user_master
获取数据的SQL查询String str = "select od.total, od.invoice_link, od.status, ";
str += "od.order_type, um.name as sender_name, umr.name as receiver_name, ";
str += "umr.twitter_handle as receiver_twitter_handle, um.twitter_handle as sender_twitter_handle ";
str += "from orders od ";
str += "join user_master um on um.id = od.user_id ";
str += "join user_master umr on umr.id = od.receiver_id ";
str += "where user_id = '"+user_id+"' ";
String json = dao.getResultJSON(str);
return json;
在数据库中有一个名为order_type的列,其中包含transfer,request和invoice等值。如果order_type列中的值为'transfer'且'Requested from',则需要将“Payment to”返回到变量json中如果列中的值是'request'。请忘记我上面提到的'发票'。这两个就足以满足我的预期输出..
无论如何,提前谢谢
答案 0 :(得分:1)
在不知道payment_to
和requested_from
应该来自何处的情况下,这是在黑暗中拍摄的:
select
od.total
, od.invoice_link
, od.status
, od.order_type
, um.name as sender_name
, umr.name as receiver_name
, um.twitter_handle as sender_twitter_handle
, umr.twitter_handle as receiver_twitter_handle
, case when od.order_type = 'transfer' then od.payment_to else null end as payment_to
, case when od.order_type = 'request' then od.requested_from else null end as requested_from
from orders od
inner join user_master um on um.id = od.user_id
inner join user_master umr on umr.id = od.receiver_id
where user_id = '"+user_id+"'
以您发布的格式:
String str = "select";
str += " od.total";
str += " , od.invoice_link";
str += " , od.status";
str += " , od.order_type";
str += " , um.name as sender_name";
str += " , umr.name as receiver_name";
str += " , um.twitter_handle as sender_twitter_handle ";
str += " , umr.twitter_handle as receiver_twitter_handle";
str += " , case when od.order_type = 'transfer' then od.payment_to else null end as payment_to";
str += " , case when od.order_type = 'request' then od.requested_from else null end as requested_from";
str += " from orders od ";
str += " inner join user_master um on um.id = od.user_id ";
str += " inner join user_master umr on umr.id = od.receiver_id ";
str += " where user_id = '"+user_id+"';";
String json = dao.getResultJSON(str);
return json;
答案 1 :(得分:0)
@model VotingWebApp.Models.QuestionItem
@{
ViewData["Title"] = "Answer Question";
}
<h2>@Html.DisplayFor(model => model.Question)</h2>
<div class="container">
<button type="button" class="btn btn-info btn-lg" onclick= "CALL AnswerASelect(Model)">@Html.DisplayFor(model => model.AnswerA)</button>
是从看似Java的SQL解析出来的。您应该了解SQL在表中处理;也就是说,它处理充满数据的矩形。每个结果集都返回一系列行。结果集中的每一行必须与其他行具有相同的列。
您要求提供两种不同的行,一行包含select od.total,
od.invoice_link, od.status,
od.order_type,
um.name as sender_name, umr.name as receiver_name,
umr.twitter_handle as receiver_twitter_handle,
um.twitter_handle as sender_twitter_handle
from orders od
join user_master um on um.id = od.user_id
join user_master umr on umr.id = od.receiver_id
where user_id = SOMETHING
列,另一行包含Payment to
列。行的选择取决于行中的某些数据。这不会发生在单个查询中。
你可以做这样的事情。
Requested from
这将在结果集的每一行中生成所需的两列。当不相关时,列中的一列或两列将包含空格。
这些select
...,
CASE WHEN od.order_type='transfer' THEN receiver_twitter_handle ELSE '' END `Payment to`,
CASE WHEN od.order_type='request' THEN sender_twitter_handle ELSE '' END `Requested from`,
...
语句的详细信息可能因表服务器的不同而有所不同,您还没有透露您正在使用哪一个,因此这里可能存在错误。但你可以解决它。