我有一张表A 主键id(自增)
令外有表B、C、D 他们的第一列都是A的主键ID的值(A的主键ID只可能在B、C、D的一个中出现)我现在想实现这样的功能找出A的前10条记录和匹配到的表明。非常感谢。
令外有表B、C、D 他们的第一列都是A的主键ID的值(A的主键ID只可能在B、C、D的一个中出现)我现在想实现这样的功能找出A的前10条记录和匹配到的表明。非常感谢。
解决方案 »
- mysqlreport结果报告
- mysql多线程读写方案求解
- 索引引用不到(求高手赐教)
- 有没有哪位高手知道如何把access数据库文件(扩展名为MDB)转换为标准的SQL格式?
- mysql 这样的如何优化
- 请教个mysql语句的问题
- 请问如何在windows下同时运行两个不同版本的mysql?
- log4j写日志进mysql的中文乱码问题,在sql 2000 下面没什么问题。求救啊!
- 求救!为何在win2000server中装不了mysql??????????????????
- 两表查询的SQL问题
- select 字段 from 与select * from速度差很多问题
- MySql 存储过程的语法
from a
left join b on a.id=b.aid
left join c on a.id=c.aid
left join d on a.id=d.aid
order by a.id
limit 10;
个人愚见 if(b.aid is not null,'B',if(c.aid is not null,'C','D')) 这句应该可以不要 但我也是小菜
from A left join (
select 'B' as t,id from B
union all
select 'C' as t,id from B
union all
select 'D' as t,id from B
) u on a.id=u.id
select a1.id,b1.bz from A a1 left join (
select 'B' as bm,id from b
union all
select 'C' as bm,id from c
union all
select 'D' as bm,id from d
) b1 on a1.id=b1.id order by a1.id desc limit 10orselect a1.id,b1.bz from A a1 left join (
select 'B' as bm,id from b
union all
select 'C' as bm,id from c
union all
select 'D' as bm,id from d
) b1 on a1.id=b1.id where b1.id is not null order by a1.id desc limit 10
from A left join (
select 'B' as t,id from B
union all
select 'C' as t,id from B
union all
select 'D' as t,id from B
) u on a.id=u.id order by a1.id desc limit 10
(select id from c) as cid,
(select id from d) as did
from A
limit 10b,c,d 表中创建基于ID的索引。
谢谢,创建索引貌似也有点慢这得union多少数据啊。
WHEN c.id IS NOT NULL THEN 'C'
ELSE 'D' END AS bm
FROM a
LEFT JOIN b ON a.id=b.id
LEFT JOIN c ON a.id=c.id
LEFT JOIN d ON a.id=d.id
ORDER BY a.id DESC LIMIT 10