我正在按照以下方式按日期和时间对数据进行排序
UIPasteboard.general.string = myVoucher.voucherCode
这是我的表格结构 我从SELECT之一中获取mysql错误号#1250表'a1'不能在字段列表中使用
如何根据我的情况正确地使用unioun子句对数据进行排序?
答案 0 :(得分:1)
您可以在下面尝试-您需要在外部查询中使用order by子句
select * from
(
select b.app_user_id,b.username,a.message_content,a.message_to,a.message_date
from app_messages a
left join app_users b
on a.message_from = b.app_user_id
where a.message_to=1 and b.app_user_id= 4
UNION
select b1.app_user_id,b1.username,a1.message_content,a1.message_to,a1.message_date
from app_messages a1
left join app_users b1
on a1.message_from = b1.app_user_id
where a1.message_to=4 and b1.app_user_id= 1
)A order by message_date
答案 1 :(得分:1)
如果没有另外指定,则结果中的列名将由联合的第一个查询确定。因此,此查询应执行您想要的操作:
select b.app_user_id,b.username,a.message_content,a.message_to,a.message_date
from app_messages a
left join app_users b
on a.message_from = b.app_user_id
where a.message_to=1 and b.app_user_id= 4
UNION
select b1.app_user_id,b1.username,a1.message_content,a1.message_to,a1.message_date
from app_messages a1
left join app_users b1
on a1.message_from = b1.app_user_id
where a1.message_to=4 and b.app_user_id= 1
order by message_date;