-2

I've got 2 tables that I want to join together where the id of the second table matches the id of the first table and is outputted together as row.

The first table is "boys" and has a column of "id" and "name".
The second table is "girls" and has a column of "id" and "name".

How do I get the matching ids to join as a single row when output in PHP? I went through a whole basic SQL tutorial about joins and am having trouble wrapping my head around the logic for some reason. Any help ?

$result = mysql_query("
SELECT *
FROM boys
LEFT JOIN girls
WHERE boys.id = girls.id
");

Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\xampp\main3.php on line 28

Brian Tompsett - 汤莱恩
  • 5,438
  • 68
  • 55
  • 126
Steven
  • 1

3 Answers3

0

Which join you would like. If you prefer inner join then try this

SELECT Orders.OrderID, Customers.CustomerName
FROM Orders
INNER JOIN Customers ON Orders.CustomerID = Customers.CustomerID;

for left join

SELECT Customers.CustomerName, Orders.OrderID
FROM Customers
LEFT JOIN Orders ON Customers.CustomerID = Orders.CustomerID
ORDER BY Customers.CustomerName;

and for right join

SELECT Orders.OrderID, Employees.LastName, Employees.FirstName
FROM Orders
RIGHT JOIN Employees ON Orders.EmployeeID = Employees.EmployeeID
ORDER BY Orders.OrderID;
Haq Nawaz
  • 302
  • 2
  • 11
  • Can you add some detail as to what the difference between these joins is? The OP is struggling to understand that – Caius Jard Aug 02 '17 at 09:28
  • Could you translate that into code with my table/column names ? – Steven Aug 02 '17 at 09:34
  • (INNER) JOIN: Returns records that have matching values in both tables LEFT (OUTER) JOIN: Return all records from the left table, and the matched records from the right table RIGHT (OUTER) JOIN: Return all records from the right table, and the matched records from the left table Thanks – Haq Nawaz Aug 02 '17 at 09:34
  • That's the tutorial I read over and over. Welp, thanks anyway Haq ! – Steven Aug 02 '17 at 09:35
  • Do you a girl_id in boys table? – Haq Nawaz Aug 02 '17 at 09:38
  • If you have a girl_id in boys table then the query will SELECT * FROM boys LEFT JOIN girls WHERE boys.girl_id = girls.id – Haq Nawaz Aug 02 '17 at 09:41
  • No, I don't have a "girls_id" in the "boys table. Do I need one ? I have "id" in both the "boys" and "girls "table". – Steven Aug 02 '17 at 09:46
  • Yes you need parent child relationship. On this time I don't now what is your scenario. If you want for one boy have multiple girls then you need to put a a boy primary id in girs table as foreign key id. If you want one girls have multiple boys then put girl primary key id in boys table as a foreign key. – Haq Nawaz Aug 02 '17 at 09:51
0

You only provided your query, that's fairly thin to work with.

According to your error, you give mysql_fetch_array a boolean to work with, which is incorret.

The reason why you give mysql_fetch_array a boolean is certainly that your query failed and still you pass it to mysql_fetch_array without checking for the validity of its return.

Anyway, merely using mysql_fetch_array and the other mysql_* functions is a security flaw in itself. You should move to PDO as soon as you can.

Sarkouille
  • 1,265
  • 9
  • 16
-1

Try this:

SELECT b.*, c.* FROM boys b, girls c WHERE b.id=c.id
King18Ash
  • 1
  • 4