在每个表,mysql,经典asp中按相同字段排序2个表

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在每个表,mysql,经典asp中按相同字段排序2个表相关的知识,希望对你有一定的参考价值。

我试图从2个表中排序数据,以便根据第三个表中的id显示在一个列表中。

表3包含其他2个表的ID。

e_allocation table
------------------
eid, sid, iid

5, 1234, NULL

5, NULL, 1234

表1和表2是类似数据的表。

i_status table
--------------
iid, status

1234, Complete

1235, Complete

1236, Not Complete
s_status table
--------------
sid, status

1234, Complete

1235, Not Complete

1237, Not Complete
objCon.Execute ("SELECT a.sid, a.iid, b.status AS istats, c.status AS sstats
FROM e_allocation a LEFT JOIN i_status b ON a.iid=b.iid LEFT JOIN s_status c
ON a.sid=c.sid WHERE a.eid = '5' ORDER BY b.status Desc, c.status Desc")

所以目前我可以从数据库中获取数据,但它显然首先命令表b,然后是c秒。我需要这些订购加入。

目前返回(Table=I or S, Record ID=1234, Status = Complete or Not Complete)

I:1234:Complete

I:1235:Complete

I:1236:Not complete

S:1234:Complete

S:1235:Not Complete

S:1237:Not Complete

我想要:

I:1234:Complete

S:1234:Complete

I:1235:Complete

S:1235:Not Complete

I:1236:Not complete

S:1237:Not Complete
while not objDb.EOF
    sid = objDb("id")
    iid = objDb("iid")
    if sid <>"" then datlst = datlst &"S:"& sid &":"&objDb("sstats")&",<BR>"
    if iid <>"" then datlst = datlst &"I:"& iid &":"&objDb("istats")&",<BR>"
    objDb.MoveNext
Wend
response.write datlst

任何正确方向的指针都非常赞赏?谢谢

答案

这是我的尝试。

create table e_allocation (eid int, sid int, iid int);
insert into e_allocation select 5, 1234, NULL;
insert into e_allocation select 5, NULL, 1234;
insert into e_allocation select 5, 1235, NULL;
insert into e_allocation select 5, NULL, 1235;
insert into e_allocation select 5, 1236, NULL;
insert into e_allocation select 5, NULL, 1236;

create table i_status (iid int, status varchar(25));
insert into i_status select 1234, 'Complete';
insert into i_status select 1235, 'Complete';
insert into i_status select 1236, 'Not Complete';

create table s_status (sid int, status varchar(25));
insert into s_status select 1234, 'Complete';
insert into s_status select 1235, 'Not Complete';
insert into s_status select 1237, 'Not Complete';

SELECT
  case when a.sid is null then 'I' else 'S' end as iors,
  ifnull(a.sid,a.iid) as id,
  ifnull(b.status,c.status) as status
FROM e_allocation a LEFT JOIN i_status b ON a.iid=b.iid LEFT JOIN s_status c
ON a.sid=c.sid
WHERE a.eid = '5' and (b.status is not null or c.status is not null)
ORDER BY id, iors

另一次请提供CREATE TABLE等语句,以便我们不必输入它们。

以上是关于在每个表,mysql,经典asp中按相同字段排序2个表的主要内容,如果未能解决你的问题,请参考以下文章

在 Eloquent 中按字段排序 MySQL

从 yii 2 中的其他表中按 count(*) 排序

mysql:group by,order by

Access 2010 SQL--在交叉表查询中按聚合函数对行进行排序

如何在 Eloquent ORM 中按多对多关系的数据透视表的字段进行排序

如何在数据库表中按字母顺序对列进行排序? [关闭]