简体   繁体   English

尝试从MySQL查询的两个表中获取信息

[英]Trying to get info from two tables with MySQL query

My page info and images are stored in two separate tables. 我的页面信息和图像存储在两个单独的表中。 I want 'type' and 'body' from the table "page". 我想从表格“页面”中找到“类型”和“正文”。 Then 'images' from the table "page_images". 然后从表“page_images”中“图像”。 Both obviously from the same page. 两者显然来自同一页面。

I want a few teasers of random pages. 我想要一些随机页面的戏弄。 But, I need ONE image (even though many are stored for each page) from "page_images" and the 'type' and 'body' from "page". 但是,我需要一个图像(即使每个页面存储了很多图像)来自“page_images”,而'type'和'body'来自“page”。 (images are stored as 'image_loc' in the table page_images) (图像在表page_images中存储为'image_loc')

-- TABLE  page

page_id | user_id | url_title     | title         | type | full_location | phone | body
----------------------------------------------------------------------------------------
1       | 1       | jazz_showcase | Jazz Showcase | Jazz | ...           | ...   | ... 

-- TABLE  page_images

image_id | user_id | page_title     | image_loc
-----------------------------------------------
1        | 1       | jazz_showcase  | ...
2        | 3       | jazz_showcase  | ...
3        | 2       | something_else | ...

This query doesn't work... 此查询无效...

function OnTheSpot() {
        $query = mysql_query("SELECT page.body AS desc, page.style AS styles, page_images.image_loc AS images
                                FROM page
                                LEFT JOIN page_images
                                ON page.url_title = page_images.page_title 
                                ORDER BY RAND()
                                LIMIT 1
                            ");
        while($fetch = mysql_fetch_assoc($query)) {
            $desc = $fetch['desc'];
            $styles = $fetch['styles'];
            $images = $fetch['images'];
            echo $desc.'<br />';
            echo $styles.'<br />';
            echo $images.'<br />';
            }
        }

I would want 5 or so random pages demoed. 我想要5个左右的随机页面进行演示。 Each page with a limit of one image and the type and body displayed. 每个页面都限制一个图像,并显示类型和主体。 How would this be query'd. 如何查询。

Change the desc to eg descr . desc更改为例如descr Desc is a reserved word in MySQL and you can't use it. Desc是MySQL中的保留字,您无法使用它。

See: MySQL Reserved Words 请参阅: MySQL保留字

EDIT: Here is also a query for you, which probably solves your problem: 编辑:这里也是一个查询,可能会解决您的问题:

$query = "SELECT page.body AS descr, page.url_title, page.style AS styles, page_images.image_loc AS images FROM page,page_images WHERE page_images.page_title = page.url_title ORDER BY RAND() LIMIT 1");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM