LEFT JOIN从表1打印第一条记录,然后从表2打印任何/所有联接的记录,然后从表1打印第二条记录,依此类推

时间:2018-07-27 20:40:58

标签: php mysql

此代码为table1中的每个记录返回table2中的一行。我是否需要创建两个SELECT语句来打印来自table2的所有联接行?

<?php
$sql = new MySQLi(localhost, x, y, z);

$result = $sql->query("SELECT ARTICLES.ID,ARTICLES.ARTICLE,ARTICLES.TYPE,ARTICLES.YEARLABEL,ARTICLES.ZERO,ARTICLES.TXYZ,ARTICLES.EDGES,ARTICLES.ABSTRACT,ARTICLES.DEVELOPMENTS,ARTICLES.CHERRYPICKER,ARTICLES.CONTRACTIONS,ARTICLES.WORKS,ARTICLES.RESOURCE,ASPECTS.CLASS,ASPECTS.ASPECT,ASPECTS.ARESOURCE
FROM ARTICLES
LEFT JOIN ASPECTS ON ARTICLES.ID = 
ASPECTS.LID
WHERE ARTICLES.TYPE LIKE '%Composite%'
ORDER BY ZERO ASC, ARTICLE ASC");
for ($set = array (); $row = $result->fetch_assoc(); $set[] = $row);
print nl2br(print_r($set, true));
?>

在表2中有多个条目的情况下,输出应类似于以下内容:

    1 =>
    (
    [ID] => 223
    [ARTICLE] => Hero of Alexandria (Heron)
    [TYPE] => Composite
    [YEARLABEL] => AD
    [ZERO] => 10
    [TXYZ] => 10-70 circa Roman Egypt
    [ELEMENTS] => 
    [EDGES] => invention, math, mensuration, optics
    [ABSTRACT] => Greek scientist whose interests included optics..
    [DEVELOPMENTS] => aeolipile (may have been described a century earlier)
    [CHERRYPICKER] => The aeolipile Hero described is considered to be the first recorded steam engine or reaction steam turbine.
    [CONTRACTIONS] => 
    [WORKS] => 
    [RESOURCE] => 
        Array (table2:ASPECTS)
            0=>
                'AID' => 101
                'CLASS' => Hero's method
                'ASPECT' => An iterative method of approximating the square root of a number.
                'AREOURCE' => null
            1=>
                'AID' => 102
                'CLASS' => Hero's formula
                'ASPECT' => In geometry, formula gives the area of a triangle by requiring no arbitrary choice of side as base or vertex as origin, contrary to other formulas for the area of a triangle, such as half the base times the height or half the norm of a cross product of two sides.
                'AREOURCE' => null


    SHOW CREATE TABLE `ARTICLES` //Table1

ARTICLES    CREATE TABLE `ARTICLES` (
  `ID` int(11) NOT NULL AUTO_INCREMENT,
  `ARTICLE` longtext COLLATE utf8mb4_unicode_ci,
  `TYPE` longtext COLLATE utf8mb4_unicode_ci,
  `YEARLABEL` longtext COLLATE utf8mb4_unicode_ci,
  `ZERO` float DEFAULT NULL,
  `TXYZ` longtext COLLATE utf8mb4_unicode_ci,
  `ELEMENTS` longtext COLLATE utf8mb4_unicode_ci,
  `EDGES` longtext COLLATE utf8mb4_unicode_ci,
  `ABSTRACT` longtext COLLATE utf8mb4_unicode_ci,
  `DEVELOPMENTS` longtext COLLATE utf8mb4_unicode_ci,
  `CHERRYPICKER` longtext COLLATE utf8mb4_unicode_ci,
  `CONTRACTIONS` longtext COLLATE utf8mb4_unicode_ci,
  `WORKS` longtext COLLATE utf8mb4_unicode_ci,
  `RESOURCE` longtext COLLATE utf8mb4_unicode_ci,
  PRIMARY KEY (`ID`)
) ENGINE=MyISAM AUTO_INCREMENT=9198 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci  

SHOW CREATE TABLE `ASPECTS`/Table2

ASPECTS CREATE TABLE `ASPECTS` (
  `AID` int(11) NOT NULL AUTO_INCREMENT,
  `LID` int(11) DEFAULT NULL,
  `CLASS` longtext COLLATE utf8mb4_unicode_ci,
  `ASPECT` longtext COLLATE utf8mb4_unicode_ci,
  `ARESOURCE` longtext COLLATE utf8mb4_unicode_ci,
  PRIMARY KEY (`AID`)
) ENGINE=MyISAM AUTO_INCREMENT=5355 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci  

这是我搜索后可以找到的最接近的代码。 SQL执行。 PHP没有。

<?php

include('mysqliconfig.php');

$conn = mysqli_connect($servername, $username, $password, $dbname) or die("Error " . mysqli_error($conn));

$result = mysqli_query($conn, SELECT ARTICLES.ID,ARTICLES.ARTICLE,ARTICLES.TYPE,ARTICLES.YEARLABEL,ARTICLES.ZERO,ARTICLES.TXYZ,ARTICLES.ELEMENTS,ARTICLES.EDGES,ARTICLES.ABSTRACT,ARTICLES.DEVELOPMENTS,ARTICLES.CHERRYPICKER,ARTICLES.CONTRACTIONS,ARTICLES.WORKS,ARTICLES.RESOURCE, ASPECTS.CLASS,ASPECTS.ASPECT,ASPECTS.ARESOURCE,ASPECTS.AID as NID FROM ARTICLES 
LEFT JOIN ASPECTS ON ARTICLES.ID = ASPECTS.LID 
WHERE TYPE = 'Composite'
OR TYPE = 'Composite[d]'
OR TYPE = 'Composite[t]'
ORDER BY ARTICLES.ZERO, ASPECTS.AID");
$array = array('ASPECTS'=>array());
$i=0;
$lastid = null;
while($row=mysqli_fetch_object($result))
{
 if($lastid!==$row->ID)
 {
  $array['ARTICLES'][++$i]=array('ARTICLE'=>$row->time,'TYPE'=>$row->name,'LINKED'=>array());
  $lastid=$row->ID;
 }
 if($row->AID!==null)
 {
  $array['ASPECTS'][$i]['LINKED'][]=array('CLASS'=>$row->title,'ASPECT'=>$row->short_desc,'ARESOURCE'=>$row->desc);
 }
}
?>

0 个答案:

没有答案