简体   繁体   English

MySQL 有条件的 select 语句?

[英]MySQL conditional select statement?

Okay, so I have a client who wants to output all information for a bunch of students in a system.好的,所以我有一个客户想要 output 系统中一堆学生的所有信息。

This output is to be imported into Excel to be manipulated.这个 output 将被导入到 Excel 中进行操作。

So, my problem is that I would like to output all students' information from 4 tables所以,我的问题是我想 output 来自 4 个表的所有学生信息

auditions scheduled auditions profiles audition times试镜 预定试镜 简介 试镜时间

So, the auditions table gets and entry linking any information there to the student WHEN the audition is recorded.因此,当试听被记录时,试听表会获取并将那里的任何信息链接到学生的条目。 Before that, there is no audition for them in the db.在此之前,数据库中没有他们的试镜。

So, I want to output one row for each student that says when their scheduled audition is, and then if they have had their audition already to import the information, but if not, to just leave the field blank.所以,我想 output 为每个学生写一行,说明他们计划的试镜时间,然后如果他们已经进行了试镜,则导入信息,但如果没有,则将该字段留空。

I've got something like this so far到目前为止我有这样的东西

"SELECT ".$fields." FROM profiles p, auditions a, scheduled_auditions s, 
audition_times t WHERE p.id=t.id AND t.id=s.id AND a.id=p.id"

The problem is obvious.问题很明显。 It's only going to output people who have auditions.它只适用于有试镜的 output 人。 So, how do I write a conditional select/where statement so to speak?那么,可以这么说,我该如何编写条件选择/where 语句? I need to output this with one query so that it will work when I open it in excel.我需要用一个查询来 output ,这样当我在 excel 中打开它时它就可以工作。

Thanks guys.多谢你们。 I'm stumped.我难住了。

Have you considered using a LEFT OUTER JOIN ?您是否考虑过使用LEFT OUTER JOIN

If you join tables A and B with an inner join, you only get rows that exist in A and B. If you use a LEFT JOIN you would get all of the rows in A. Some of those rows would have data from B if it existed.如果使用内部连接连接表 A 和 B,则只会获得 A 和 B 中存在的行。如果使用 LEFT JOIN,您将获得 A 中的所有行。如果它,其中一些行将包含来自 B 的数据存在。 Otherwise those columns from B would be null.否则 B 中的那些列将是 null。

EDIT: A LEFT JOIN B is logically equivalent to B RIGHT JOIN A - so do whatever makes the most sense in your head (Thanks Paul.).编辑: A LEFT JOIN B在逻辑上等同于B RIGHT JOIN A - 所以做你头脑中最有意义的事情(谢谢 Paul。)。

You want to perform an outer join;您要执行外部联接; an outer join returns results even when the joined records are null.即使连接的记录是 null,外连接也会返回结果。

Specifically, specifying a LEFT OUTER JOIN in your case on the auditions tables will force all the students' records to be returned, even the ones without auditions.具体来说,在您的情况下,在试镜表上指定 LEFT OUTER JOIN 将强制返回所有学生的记录,即使是没有试镜的记录。

Sounds like you just need an outer join, which is like a normal join, but where there is no matching key it will just return NULL for every field.听起来你只需要一个外连接,这就像一个普通的连接,但是如果没有匹配的键,它只会为每个字段返回 NULL。

SELECT * 
FROM profiles p
LEFT OUTER JOIN audition_times t
ON p.id=t.id
LEFT OUTER JOIN scheduled_auditions s
ON t.id=s.id 
LEFT OUTER JOIN auditions a
ON a.id=p.id"

You need to use an outer join for this.为此,您需要使用外部联接。 Look at http://en.wikipedia.org/wiki/Join_(SQL)#Outer_joins看看http://en.wikipedia.org/wiki/Join_(SQL)#Outer_joins

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

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