简体   繁体   中英

MySql query combine results from 2 tables

I try to combine data from 2 tables by a mysql join but I look likes, I don't receive the results from the second table at all.

Table structre #1 (site_hosters);

+------------+--------------+--------+
| host_id    | name         | prio   |
+------------+--------------+--------+
| 1          | site.com     | 0      |
+------------+--------------+--------+

Table structure #2 (site_date);

+------------+--------------+--------+
| id         | hoster       | page   |
+------------+--------------+--------+
| 1          | site.com     | http:..|
+------------+--------------+--------+

What I try to get is a result like 'id, host_id, name, ....etc';

When I try the follow query, it doesn't take host_id and prio in the query result. It looks, the join has no effect at all by query it.

My query:

SELECT 
         site.id,
         site.hoster,
         site.page,

FROM site_data as site

INNER JOIN site_hosters hoster
ON site.hoster = hoster.name

I hope someone can help me with this one.

Kind regards, Nick

You have to name the columns you want to select. Add the site_hosters columns like that:

SELECT 
     site.id,
     site.hoster,
     site.page,
     hoster.host_id,
     hoster.prio
FROM site_data as site
INNER JOIN site_hosters hoster ON site.hoster = hoster.name

You could do

SELECT
           site.*,
           hoster.*
FROM site_data as site  
INNER JOIN site_hosters hoster ON site.hoster = hoster.name 

This will return all fields, however, you may only want something like

site.id, ste.hoster, site.page, hoster.prio as your fields.

Just list required fields in SELECT:

SELECT site.id, hoster.host_id, hoster.name, site.hoster, site.page
FROM site_data AS site
INNER JOIN site_hosters AS hoster ON site.hoster = hoster.name

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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