简体   繁体   English

协助创建 SQL 查询

[英]assistance creating a SQL Query

Somewhat new to SQL queries and I need a little help with my join. SQL 查询有点新,我的加入需要一些帮助。

I am supplied with gid我提供了gid

For each of these I need to grab name from table wp_ngg_gallery对于这些中的每一个,我需要从表wp_ngg_gallery 中获取名称

then join in table wp_ngg_pictures and grab field filename limit 1 order DESC by field imagedate然后通过现场imagedatewp_ngg_pictures和抢实地1DESC加盟

替代文字

替代文字

Anyone able to help?任何人都可以提供帮助?

Let me know how / if this works.让我知道这如何/是否有效。 If you dump both of those tables (out of phpMyAdmin) in a SQL format I will create local databases and give it a run myself (assuming this doesn't work).如果您以 SQL 格式转储这两个表(从 phpMyAdmin 中),我将创建本地数据库并自己运行(假设这不起作用)。

SELECT
    `wp_ngg_gallery`.`name`,
    `wp_ngg_pictures`.`filename`
FROM
    `wp_ngg_gallery`
    LEFT JOIN
        `wp_ngg_pictures`
    ON
        `wp_ngg_gallery`.`gid` = `wp_ngg_pictures`.`galleryid`
ORDER BY
    `wp_ngg_pictures`.`imagedate` DESC
LIMIT
    0,1;

This is assuming you need a JOIN.这是假设您需要 JOIN。 It could probably be done with a decent GROUP BY statement.它可能可以通过一个像样的GROUP BY语句来完成。

I'm going to guess:我会猜测:

select wp_ngg_gallery.name, wp_ngg_pictures.filename
from wp_ngg_gallery, wp_ngg_pictures
where wp_ngg_pictures.galleryid = wp_ngg_gallery.gid
order by wp_ngg_pictures.imagedate DESC 
limit 0,1

If there's a specific GID you want, you could do:如果你想要一个特定的 GID,你可以这样做:

select wp_ngg_gallery.name, wp_ngg_pictures.filename
from wp_ngg_gallery, wp_ngg_pictures
where wp_ngg_pictures.galleryid = wp_ngg_gallery.gid
and wp_ngg_gallery.gid = $gid
order by wp_ngg_pictures.imagedate DESC 
limit 0,1

(The $gid is valid if you're query is a string in PHP, you didn't say, though, if that's what you're using) (如果您的查询是 PHP 中的字符串,则 $gid 是有效的,但是您没有说,如果这是您正在使用的)

It's not clear if wp_ngg_pictures.galleryid is a foreign key to wp_ngg_gallery.gid but it's the most likely choice given the info you supplied.目前尚不清楚 wp_ngg_pictures.galleryid 是否是 wp_ngg_gallery.gid 的外键,但鉴于您提供的信息,它是最有可能的选择。 If it's not (if you can have galleries with no images), you might want to modify the other guy's query (using the outer join) by adding and wp_ngg_gallery.gid = $gid to it.如果不是(如果您可以拥有没有图像的画廊),您可能希望通过添加and wp_ngg_gallery.gid = $gid来修改其他人的查询(使用外连接)。

If you want it for multiple gids (choose the other options for a single one):如果您希望它用于多个gid(为单个gid 选择其他选项):

SELECT gid,name, a.filename
FROM wp_ngg_gallery
JOIN  wp_ngg_pictures a
ON a.galleryid = gid 
LEFT JOIN wp_ngg_pictures b
ON a.galleryid = gid 
AND b.imagedate > a.imagedate
WHERE b.galleryid IS NULL
AND gid IN (1,2,3)

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

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