Reputation:
I would like to ask assistance to help me solve my coding problem, I would like to obtain information from two tables in the same database.
DB structure:
Table Name: _net
id
name
ip
comment
I can get all relevant details from this table with the following code:
$netsql = 'SELECT * FROM _net ORDER BY id';
$qnet = $conn->query($netsql);
$qnet->setFetchMode(PDO::FETCH_ASSOC);
Table: _net_port (other information I require to do a query)
id
netid
port
comment
I would like to obtain the information from table _net_port where the relevant id = to netid (id in Table _net will be the same as netid in Table _net_port. Especially to obtain the list of ports.
Upvotes: 1
Views: 62
Reputation: 3733
You need JOIN
SELECT * FROM _net ORDER BY id
INNER JOIN _net_port ON _net_port.id = _net.id
Your question is about MySQL, not about PHP script.
Upvotes: 1
Reputation: 311188
You could join the _net
table on an aggregate query of _net_port
with group_concat
:
SELECT n.*, p.ports
FROM _net n
JOIN (SELECT netid, GROUP_CONCAT(port)
FROM _net_ports
GROUP BY netid) p ON n.id = p.netid
Upvotes: 1