多表查询问题 left,rigth join 问题

wxlwxlwxlwxl 2008-03-07 03:04:05
是这样两个表 tfile(fileid,filename,filepath,subjectid),tsubject(subjectid,subjectname)
假定 tfile表数据为
fileid filename filepath subjectid
1 file1 img/file1.jpg 1
2 file2 img/file2.jpg 1
3 file3 img/file3.jpg 1

tsubject表数据为
subjectid subjectname
1 photo
2 life

我要实现这种效果

fileid filename filepath subjectid subjectname
3 file3 img/file3.jpg 1 photo
null null null 2 life

我现在只是实现了
fileid filename filepath subjectid subjectname
3 file3 img/file3.jpg 1 photo

我是这样写的
select fileid,filepath,filename,subjectid,subjectname from tfile a right join tsubject b on a.subjectid =b.subjectid where a.fileid in
( select max(fileid) from tfile c inner join tsubject d on c.subjectid =d.subjectid group by c.subjectid )

这个问题怎么解决啊?

...全文
142 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
wxlwxlwxlwxl 2008-03-07
  • 打赏
  • 举报
回复
解决了,多谢各位
我把我那个稍微改了下也没问题了
:)
wxlwxlwxlwxl 2008-03-07
  • 打赏
  • 举报
回复
多谢各位,我先试下。
flyidealism 2008-03-07
  • 打赏
  • 举报
回复
---建立测试数据
create table tfile(fileid int,filename varchar(100),filepath varchar(50),subjectid int)

insert into tfile
select 1 ,'file1','img/file1.jpg',1
union all
select 2,'file2','img/file2.jpg',1
union all
select 3,'file3','img/file3.jpg',1


create table tsubject(subjectid int,subjectname varchar(50))
insert into tsubject
select 1,'photo'
union all
select 2,'life'

--SQL代码实现
select a.fileid,a.filename,a.filepath,b.subjectid,b.subjectname
from
(select * from tfile
where fileid=3) a right join tsubject b
on a.subjectid=b.subjectid
--结果如你要求
nzperfect 2008-03-07
  • 打赏
  • 举报
回复
create table tfile(fileid int,filename varchar(20),filepath varchar(30),subjectid int)
insert into tfile
select 1 ,'file1', 'img/file1.jpg', 1 union all
select 2 ,'file2', 'img/file2.jpg', 1 union all
select 3 ,'file3', 'img/file3.jpg', 1

create table tsubject(subjectid int ,subjectname varchar(20))
insert into tsubject
select 1 ,'photo' union all
select 2 ,'life'


select b.fileid,b.filename,b.filepath,
a.subjectid,a.subjectname
from tsubject as a
left join
(
select fileid,filename,filepath,subjectid from tfile as c where fileid=(select max(fileid) from tfile where subjectid=c.subjectid)
)as b on a.subjectid=b.subjectid

--
3 file3 img/file3.jpg 1 photo
NULL NULL NULL 2 life
pt1314917 2008-03-07
  • 打赏
  • 举报
回复
在SQL语句后面加上“我要带颜色显示”即可,如:

select * from 表名 我要带颜色显示
山之魂2 2008-03-07
  • 打赏
  • 举报
回复
怎么把SQL 里面的文字颜色带出来啊?
pt1314917 2008-03-07
  • 打赏
  • 举报
回复
小枪,你那连接不对吧?应该换右连接。
山之魂2 2008-03-07
  • 打赏
  • 举报
回复
select b.fileid,b.filename, b.filepath,a.*
from tfile b
right join tsubject a
on a.subjectid = b.subjectid
-狙击手- 2008-03-07
  • 打赏
  • 举报
回复
select b.fileid,
b.filename,
b.filepath,
a.*
from (select *
from tsubject t
where not exists(select 1 from tsubject where subjectid = t.subjectid and fileid> t.fileid)) a
left join tfile b on a.subjectid = b.subjectid
kk19840210 2008-03-07
  • 打赏
  • 举报
回复
create table tfile(fileid int,[filename] varchar(10),filepath varchar(20),subjectid int)


create table tsubject(subjectid int ,subjectname varchar(10))

insert into tfile values(1,'file1','img/file1.jpg',1)
insert into tfile values(2,'file2','img/file2.jpg',1)
insert into tfile values(3,'file3','img/file3.jpg',1)

insert into tsubject values(1,'photo')
insert into tsubject values(2,'life')


select fileid,filepath,[filename],c.subjectid,subjectname from tsubject c left join
(select * from tfile a where not exists(select 1 from tfile where fileid>a.fileid))d
on c.subjectid=d.subjectid



fileid filepath filename subjectid subjectname
----------- -------------------- ---------- ----------- -----------
3 img/file3.jpg file3 1 photo
NULL NULL NULL 2 life

(2 行受影响)
-狙击手- 2008-03-07
  • 打赏
  • 举报
回复
select b.fileid,
b.filename,
b.filepath,
a.*
from select *
from tsubject t
where not exists(select 1 from tsubject where subjectid = t.subjectid and fileid> t.fileid) a
left join tfile b on a.subjectid = b.subjectid
pt1314917 2008-03-07
  • 打赏
  • 举报
回复

select c.fileid,c.filename,c.filepath,b.* from
(select * from tfile a where not exists(select 1 from tfile where subjectid=a.subjectid and fileid>a.fileid))c right join tsubject b
on c.subjectid = b.subjectid

-狙击手- 2008-03-07
  • 打赏
  • 举报
回复
我要实现这种效果 

fileid filename filepath subjectid subjectname
3 file3 img/file3.jpg 1 photo
null null null 2 life


---

select b.fileid,
b.filename,
b.filepath,
a.*
from tsubject a
left join tfile b on a.subjectid = b.subjectid
这个数据库看性能不错,哪位能破解一下。Overview Absolute Database: Delphi database with SQL support.Absolute Database lets you forget the Borland Database Engine (BDE). This BDE replacement is the compact, high-speed, robust and easy-to-use database engine. With Absolute Database you will not need special installation and configuration, it compiles right into your EXE. Make your application faster and smaller with Absolute Database BDE alternative!An individual may use Absolute Database Personal in a project if he / she is the only user of this project. If your project is public or if you represent a company, you must order commercial edition of Absolute Database.Key Features: No BDE; no DLLs Single-file database SQL‘92 (DDL & DML) support Single-user and multi-user mode (file-server) Unmatched ease-of-use 100% compatibility with standard DB-aware controls Strong encryption BLOB compression Free for personal use Full source code available Royalty-free Specification Ease of Deployment No BDE, no DLLs No special installation or configuration required Database engine compiles right into EXE Single-file database format Database file could be merged with the application EXE file High Speed Advanced cache management and buffering architecture Sophisticated SQL query and filter optimizer Special optimized database engine for temporary results of SQL queries Fast B*-Tree indexes with high-speed on thousands and on millions of records Buffered transactions for batch inserts, updates and deletes In-memory tables for fast operations with data loaded into memory Optimization for read-only databases, such as databases on CD High-concurrency design for multi-threaded applications Multi-User Zero-admin file-server Automatic record and table locking Automatic detection of changes made by other users Configurable limit of concurrent connections Easy switching between single-user and multi-user mode Full transactions support with ReadCommited isolation level SQL Support SELECT with DISTINCT, INNER LEFT, RIGTH, FULL and OUTER JOIN, GROUP BY and ORDER BY clauses CREATE TABLE, DROP TABLE, ALTER TABLE statements CREATE INDEX, DROP INDEX statements INSERT, UPDATE, DELETE statements BETWEEN, IN, LIKE, IS NULL, EXISTS operators Aggregate functions COUNT,SUM,MIN,MAX,AVG Most of all SQL‘92 arithmetic, logical, date-time and string functions and operators Data types conversion by function CAST SQL scripts (multiple commands separated by ‘;‘) Parameters in queries SELECT from several databases in one query Correlated and non-correlated subqueries Compactness Small size of code added into the application by the database engine units Small database file size achieved by customizable page size, compression of table metadata and other special means Automatic free space recycling for data records, index pages, and BLOB blocks Fast BLOB data compression Short indexes for string fields Low memory consumption Compatibility Support of most of TTable field data types, including BLOB fields Full compatibility with standard DB-aware visual controls such as QuickReport, DBGrid, DBNavigator, DBImage, DBMemo, DBRichEdit and others Calculated and lookup fields Most of TTable functions including Key and Range methods Support for the IProvider interface (TClientDataSet) Functionality Unicode support in String and Memo fields Multiple-fields indexes with descending, ascending, case-sensitive and insensitive options Shareable tables Default values support Min, max and not null constraints on field values Export to SQL script (reverse engineering) Security Strong encryption of database file Variety of supported encryption algorithms: Rijndael, the Advanced Encryption Standard (AES) winner 128 bit key, Rijndael 256 bit key, Blowfish 448 bit key, Twofish 128 bit key, Twofish 256 bit key, Square, Single DES and Triple DES Reliability Repair database in case of hardware failure or operating system error caused by another application Auto-detect of database corruption and automatic repair of database file Convenience Table restructuring being performed in the easiest way keeping all the existing data Data importing from and exporting to any dataset Internationalization / localization support by use of current system locale Displaying progress during potentially slow operations with the tables and databases Utilities DBImportExport utility to transfer your existing tables from database systems with BDE driver, such as Paradox, Interbase, Access, Oracle, SQL Server to Absolute Database format and vice versa. Absolute Database Manager to manage a database easily and to perform all supported operations with tables and database file Absolute Database Manager also provides you an easy way of executing SQL queries All utilities come with full source code and can be used as an advanced examples Ease of Development Full compatibility with TTable, TQuery, TSession and TDatabase components Extensive documentation: Developer‘s Guide and Reference covering every development issue. Large number of demos illustrating wide range of use cases Prompt and responsive technical support Cost-Effectiveness 100% royalty-free, i.e. there is no additional licensing fees no matter how many times you deploy the application Flexible licensing: source code and no source code versions, multi-developers licenses Comparison with other Databases You can see here Comparision with other databases Maximum Capacity SpecificationsObject Maximum sizes / numbers Multi-User Edition Single-User Edition Bytes per string field 64,000 (also limited by page size) 64,000 (also limited by page size) Bytes per BLOB field 2GB 2GB Bytes per index 64,000 (also limited by page size) 64,000 (also limited by page size) Bytes per row 65,400 (also limited by page size) 65,400 (also limited by page size) Columns per index 10,000 10,000 Columns per table 65,000 65,000 UNIQUE indexes or constraints per table 30,000 30,000 Database size 32 TB (also limited by file system and OS) 32 TB (also limited by file system and OS) Identifier length (in characters) 255 255 Locks per connection 2,147,483,647 N/A Rows per table 2,147,483,647 2,147,483,647 Tables per database 2,147,483,647 2,147,483,647 Bytes per page 65,536 65,536 Pages per database file 2,147,483,647 2,147,483,647 Connections per database Maximum value of configured connections before creating database file (up to 2,147,483,647) 1 Records in transaction 2,147,483,647 (also limited by available RAM) 2,147,483,647 (also limited by available RAM) In-memory table size 2,147,483,647 (also limited by available RAM) 2,147,483,647 (also limited by available RAM) Requirements Target Development Environment:Delphi 4, Delphi 5, Delphi 6, Delphi 7 and C++ Builder 4, C++ Builder 5, C++ Builder 6. Trial and Personal editions of Delphi are not supported.Target OS:Windows 95/98/ME, Windows NT, 2000, XP

34,593

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧