2016-05-05 2 views
0

Ich habe gelesen, dass, wenn ich LEFT,Wie erhalten Sie Daten aus zwei Tabellen, wenn sie Übereinstimmungen haben und wenn nicht?

JOIN verwende

Jedes Element in der linken Tabelle wird in einem MySQL-Ergebnis zeigen, auch wenn es keine Übereinstimmung mit dem anderen Tisch ist, dass es verbunden wird zu.

Und das ist, was ich plane, in meiner Anfrage zu verwenden, da ich Spiele von alumni.name mit profile.name finden will und immer noch alle Datensätze aus alumni Tabelle führen, und nur Datensätze aus profile Tabelle abgestimmt. So habe ich LEFT JOIN verstanden. Habe ich es richtig verstanden? Sag mir, wenn ich es nicht tue.

Wenn ich richtig bin, dann habe ich ein anderes Problem. Weil ich möchte, dass, wenn alumni.name nicht mit profile.name übereinstimmen, Datensätze aus zwei Tabellen weiterhin gedruckt werden.

So etwas wie dieses

if alumni.name==profile.name 
    output alumni.name 
else if alumni.name != profile.name 
    output alumni.name and profile.name 

Dies ist nur ein Beispiel, wie ich es tun möchte. Beurteile meinen Zustand nicht, ich weiß, dass das nicht der richtige Weg ist, aber meine Idee ist so. Hoffe, du kannst mir helfen.

Beispiel Schnipsel:

<!DOCTYPE html> 
 
<html> 
 
<body> 
 

 
<table width="30%" align="center"> 
 
<th> Alumni ID </th> 
 
<th> Alumni Firstname </th> 
 
<th> Alumni Lastname </th> 
 

 
    <tr align="center"> 
 
    <td> 1 </td> 
 
    <td>Jill</td> 
 
    <td>Smith</td> \t \t 
 
    </tr> 
 
    
 
    <tr align="center"> 
 
    <td> 2 </td> 
 
    <td>John</td> 
 
    <td>Doe</td> \t \t 
 
    </tr> 
 
    <tr align="center"> 
 
\t <td> 3 </td> 
 
    <td>Ed</td> 
 
    <td>Fin</td> \t \t 
 
    </tr> 
 

 
</table> 
 
<br><br> 
 

 
<table width="30%" align="center"> 
 
<th> Profile ID </th> 
 
<th> Profile Firstname </th> 
 
<th> Profile Lastname </th> 
 

 
    <tr align="center"> 
 
    <td> 1 </td> 
 
    <td>Ed</td> 
 
    <td>Fin</td> \t \t 
 
    </tr> 
 
    
 
    <tr align="center"> 
 
    <td> 2 </td> 
 
    <td>Ricky</td> 
 
    <td>Perez</td> \t 
 
\t 
 
\t <tr align="center"> 
 
    <td> 3 </td> 
 
    <td>Jill</td> 
 
    <td>Smith</td> \t \t 
 
    </tr> 
 
    
 
</table> 
 

 
<table width="30%" align="center"> 
 
<center><h4>EXPECTED OUTPUT </h4></center> 
 
<th> ID </th> 
 
<th> Firstname </th> 
 
<th> Lastname </th> 
 

 
    <tr align="center"> 
 
    <td> 1 (from table alumni)</td> 
 
    <td>Jill </td> 
 
    <td>Smith </td> \t \t 
 
    </tr> 
 
    <tr align="center"> 
 
    <td> 2 (from tbl alumni)</td> 
 
    <td>John</td> 
 
    <td>Doe</td> \t \t 
 
    </tr> 
 
    <tr align="center"> 
 
    <td> 2 (from tbl profile)</td> 
 
    <td>Ricky</td> 
 
    <td>Perez</td> \t \t 
 
    </tr> 
 
    <tr align="center"> 
 
    <td> 3 (from tbl alumni)</td> 
 
    <td>Ed</td> 
 
    <td>Fin</td> \t \t 
 
    </tr> 
 
    
 
</table> 
 

 

 
</body> 
 
</html>

+0

Warum zeigen Sie nicht zwei Tabellenstrukturen mit Daten und erwarteter Ausgabe an. –

+0

Sie fragen also nur, ob Ihr Verständnis von left-join korrekt ist oder fragen Sie nach einer Möglichkeit, eine Join-Abfrage in einer Datenbank mit php auszuführen? Ich sage das wegen der Tags 'php' und' mysqli' – dimlucas

+0

'CASE' ist deine Aussage: https://dev.mysql.com/doc/refman/5.7/en/case.html – mitkosoft

Antwort

0

SELECT IF (alumni.name = profile.name, alumni.name, CONCAT (alumni.name, '', profile.name)) AS Name FROM Alumni LEFT JOIN Profil auf alumni.indexkey = profile.fkey

0

Sie sollten diese Art und Weise versuchen ..

SELECT (CASE when (colname1 = "XYZ" colname2 = "ABC") 
THEN 
     1 
ELSE 
     0 
END) 
as res from mytbl; 
Verwandte Themen