从多个表中的SQLite获取数据 [英] Getting Data from multiple tables SQLite

查看:235
本文介绍了从多个表中的SQLite获取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Android应用程序有2个表,一个表存储的职位和岗位的其他表存储的图像以防它有一​​个形象的变化是不是所有的职位,将有图像,如果一个职位有一个形象的主键将存储在表的外键,加载的职位时,我必须让所有帖子阉羊与图像或没有则检查图像表中看到哪些职位有图像和图像添加到下面的帖子是的图形概览我表

I have an android application that has 2 tables, one table stores posts and the other table stores images of the posts incase it has an image, changes are not all posts will have images, if a post has an image its primary key will be stored in the foreign Key table, when loading the posts I have to get all posts wether with image or not then check in the image table to see which posts have images and add the images to the posts below is a graphical overview of my tables

后表

`|post_id |post | post_description|
 |--------|-----|-----------------|
 |        |     |                 |`

图像表

  `|img_id  |fk_postID | imagePath       |
   |--------|----------|-----------------|
   |        |          |                 |`

我可以用一个连接如         查询=选择的post_id,张贴,post_description,从的ImagePath PostTable,
                ImageTable,其中PostTable.post_id = ImageTable.fk_postID;

I could have used a join like Query = "SELECT post_id, post, post_description, imagePath FROM PostTable,
ImageTable, Where PostTable.post_id = ImageTable.fk_postID;

但这个查询只能返回与图像职位和忘却有关职位没有图像,我怎样才能获得的所有帖子可以用图片或不?先谢谢了。

but this query is only returning posts with images and forgeting about posts without images, How can I get all posts be it with image or not? Thanks in advance.

推荐答案

没关系,你问,所以给这个一抡,看看你喜欢的输出

ok, you asked, so give this a whirl, see if you like the output

SELECT pt.post_id, pt.post, pt.post_description, im.imagePath 
FROM PostTable pt
left join ImageTable im
on im.fk_postID=pt.post_id

这将带来凑凑热闹这些职位的右表(ImageTable)没有图像。

It will bring along for the ride the right table (ImageTable) of those posts that don't have images.

使用表别名 PT 即时通讯)。这有助于是明确的表的列来自第一行上的的情况下有两个共同的列名,加上少打字

Uses table aliases (pt and im). That helps to be explicit which table the columns come from on the first line in case there are common column names in both, plus a little less typing.

未测试

mysql的左连接

这篇关于从多个表中的SQLite获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆