简体   繁体   English

使用来自一个MySQL查询的结果插入到另一个查询中

[英]Using results from one MySQL query to insert into another query

I am trying to use PHP and MySQL to automatically generate a list like this. 我试图使用PHP和MySQL自动生成这样的列表。

Subject #1 主题#1

  • Resource #3 资源#3
  • Resource #5 资源#5
  • Resource #12 资源#12

Subject #2 主题#2

  • Resource #7 资源#7
  • Resource #4 资源#4
  • Resource #1 资源#1

etc. 等等

Here are the tables: 以下是表格:

eresources eresources

  • erid ERID
  • etitle etitle

subjects 主题

  • sid SID
  • stitle stitle

subjectmap subjectmap

  • sid SID
  • erid ERID

Here is the code that successfully gives me all the subjects in alphabetical order: 以下是按字母顺序成功提供所有主题的代码:

$subjectQuery = "SELECT * FROM subjects WHERE sid != 17 ORDER BY stitle ASC";
$subjectResult = $mysqli->query($subjectQuery);

while ($subjectArray = $subjectResult->fetch_assoc()) { 
print "<h5 class='subcategory'>" . $subjectArray['stitle'] . "</h5>";
print "<div class='inner'>  

// Need a list of resources that match each subject ID (sid) here!

</div>";
}

Now, here is the code that successfully gives me all the e-resources with a fixed subject ID: 现在,这里的代码成功地为我提供了具有固定主题ID的所有电子资源:

$getERBySubjectQuery = " SELECT erid FROM subjectmap WHERE sid=11 ";
$getERBySubjectResult = $mysqli->query($getERBySubjectQuery);
if($getERBySubjectResult && $getERBySubjectResult->num_rows >= 1){
 while($getERBySubjectArray = $getERBySubjectResult->fetch_assoc() ){
        $query = " SELECT * FROM eresources WHERE erid = " .$getERBySubjectArray['erid']. " ORDER BY ertitle ASC ";
    $result = $mysqli->query($query);

    if($result && $result->num_rows >= 1){
       while($array = $result->fetch_assoc() ){
          print("<a href=\"" . $array['link'] . "\">" .   "<h5 class='subcategory'>" . $array['ertitle'] . "<div class='accessnote'>"   .  $array['access']    . "</div></h5></a>");

 }}}}

Naturally, I am not inclined to duplicate the same stanza for each subject ID (sid). 当然,我不倾向于为每个主题ID(sid)复制相同的节。 I want the list of subjects to be generated automatically and the list of resources that have that subject to be generated automatically as well. 我希望自动生成主题列表,并自动生成具有该主题的资源列表。

Basically I need to feed the sid from the first stanza into the second stanza for each subject. 基本上我需要将sid从第一节提供给每个主题的第二节。

You can't do this in MySQL without executing n+1 queries where n is the number of subjects. 如果不执行n+1查询,则不能在MySQL中执行此操作,其中n是主题数。

You could just use a JOIN and some clever sorting. 你可以使用JOIN和一些聪明的排序。

SELECT
    subject.stitle, eresource.etitle
FROM subject
    LEFT JOIN subjectmap ON subject.id = subjectmap.sid
    LEFT JOIN eresource ON eresource.id = subjectmap.erid
ORDER BY subject.stitle, eresource.etitle

You will get a result like: 您将得到如下结果:

+------------+--------------+
| stitle     | etitle       |
+------------+--------------+
| Subject #1 | Resource #3  |
| Subject #1 | Resource #5  |
| Subject #1 | Resource #12 |
| Subject #2 | Resource #1  |
| Subject #2 | Resource #4  |
| Subject #2 | Resource #7  |
+------------+--------------+

By using a WHERE clause instead, I think Frits van Campen's query could be more human readable, especially for a beginner : 通过使用WHERE子句,我认为Frits van Campen的查询可能更具人性化,特别是对于初学者:

SELECT subject.stitle, eresource.etitle
FROM subject,subjectmap,eresource 
WHERE subject.id = subjectmap.sid
  AND eresource.id = subjectmap.erid
ORDER BY subject.stitle, eresource.etitle

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

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