从多个表中的SQLite获取数据多个、数据、SQLite

由网友(要走的赶紧滚)分享简介:我有一个Android应用程序有2个表,一个表存储的职位和岗位的其他表存储的图像以防它有一​​个形象的变化是不是所有的职位,将有图像,如果一个职位有一个形象的主键将存储在表的外键,加载的职位时,我必须让所有帖子阉羊与图像或没有则检查图像表中看到哪些职位有图像和图像添加到下面的帖子是的图形概览我表I have an a...

我有一个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的左连接

阅读全文

相关推荐

最新文章