mysql多选的怎么查询,结合多个高级mysql选择查询的最佳方法

I have multiple select statements from different tables on the same database. I was using multiple, separate queries then loading to my array and sorting (again, after ordering in query).

I would like to combine into one statement to speed up results and make it easier to "load more" (see bottom).

Each query uses SELECT, LEFT JOIN, WHERE and ORDER BY commands which are not the same for each table.

I may not need order by in each statement, but I want the end result, ultimately, to be ordered by a field representing a time (not necessarily the same field name across all tables).

I would want to limit total query results to a number, in my case 100.

I then use a loop through results and for each row I test if OBJECTNAME_ID (ie; comment_id, event_id, upload_id) isset then LOAD_WHATEVER_OBJECT which takes the row and pushes data into an array.

I won't have to sort the array afterwards because it was loaded in order via mysql.

Later in the app, I will "load more" by skipping the first 100, 200 or whatever page*100 is and limit by 100 again with the same query.

The end result from the database would pref look like "this":

RESULT - selected fields from a table - field to sort on is greatest

RESULT - selected fields from a possibly different table - field to sort on is next greatest

RESULT - selected fields from a possibly different table table - field to sort on is third greatest

etc, etc

I see a lot of simpler combined statements, but nothing quite like this.

Any help would be GREATLY appreciated.

解决方案

(SELECT a,b,c FROM t1)

UNION

(SELECT d AS a, e AS b, f AS c FROM t2)

ORDER BY a DESC

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值