Do I need a left, natural or simple join in SQL?

99封情书 提交于 2019-12-10 18:16:44

问题


I am new to PHP coding and just trying to fix some functionality on my site that was left over from the lead developer.

The site, [Vloggi], is a marketplace. So I need to show the name of the job poster in the assignments page . The table I have the jobs in only has the ID, not the name.

So I need a join, but I've tried and it breaks the entire site.

The SQL has 17 tables, I need to display the User Name (usr_name) contained in table 3, the organisation contained in table 7 (usrg_orgname) with the job posting user (vlop_usr_id) details in table 14.

The primary key is users.usr_id, which is linked to users_gor.usrg_usr_id and vlog-ops.vlog_usr_id.

Table 3: users usr_id, usr_email, usr_password, usr_fbuser, usr_fbtoken, usr_name, usr_loc_name, usr_loc_lat1, usr_loc_lon1, usr_loc_lat2, usr_loc_lon2, usr_status, usr_gor, usr_vgr, usr_token, usr_regtoken,

table 7: users_gor usrg_usr_id, usrg_creditops, usrg_creditvlog, usrg_creditvlogette, usrg_destination, usrg_orgname, usrg_orgtype, usrg_location, usrg_website, usrg_jobtitle, usrg_phone, usrg_address1, usrg_address2, usrg_state, usrg_postcode, usrg_country

Table 14: vlog-ops vlop_id, vlop_title, vlop_description, vlop_tags, vlop_deadline, vlop_quantity, vlop_quantityposted, vlop_vser_id, vlop_usr_id,vlop_loc_name, vlop_loc_lat1, vlop_loc_lon1, vlop_loc_lat2, vlop_loc_lon2, vlop_campaign, vlop_rules, vlop_tips, vlop_status

So in main.php i have written the following Sql lookup in main.php, I have the following SQL lookups:

$sql = "SELECT * FROM users_gor WHERE usrg_usr_id = ".$db->quote($user_info['usr_id'])." LIMIT 1";
$rows = $db->select($sql);
$users_gor = $rows[0];

$sql = "SELECT * FROM users_vgr WHERE usrv_usr_id = ".$db->quote($user_info['usr_id'])." LIMIT 1";
$rows = $db->select($sql);
$users_vgr = $rows[0];

$sql = "SELECT * FROM users WHERE usr_id = ".$db->quote($user_info['usr_id'])." LIMIT 1";
$rows = $db->select($sql);
$users = $rows[0];

$sql = "SELECT * FROM vlog-ops WHERE vlop_usr_id ".$db->quote($user_info['usr_id'])." LIMIT 1";
$rows = $db->select($sql);
$users = $rows[0];

$sql = "SELECT usr_name AS vlop_usr_name FROM users WHERE usr_id = ".$db->quote($user_info['usr_id'])." LIMIT 1";
$rows = $db->select($sql);
$users = $rows[0];

And then in the page template itself, I have written

<?php echo $vlop['vlop_vser_id'] ?>
<?php echo $vlop['vlop_usr_name'] ?>

The first one works, the second doesn’t. What I want eventually is to display the user name and the organisation name in a table.

Whenever I try a JOIN or a NATURAL JOIN or a LEFT JOIN it breaks and the entire site goes blank.

Any help for a newbie would be appreciated with a million thanks.


回答1:


When you use JOIN you need to specify how you're joining them. In the query below I'm assuming you're looking for the fields in bold from your question.

$query='SELECT u.usr_name, g.usrg_orgname, v.vlop_usr_id  FROM users u 
JOIN vlog-ops v on u.usr_id = v.vlop_usr_id 
JOIN users_gor g on u.usr_id = g.usrg_usr_id';

I believe I got the name of the fields right but if not just replace them with the correct ones.

Once you have the data fetched, you just loop through the results:

$result = mysqli_query($connection, $query);
while($row = mysqli_fetch_assoc($result)) {
    echo 'User name = ' . $row['u.usr_name'];
    echo 'Org name = ' . $row['g.usrg_orgname'];
    echo 'Job posting user id = ' . $row['v.vlop_usr_id'];
}


来源:https://stackoverflow.com/questions/49502466/do-i-need-a-left-natural-or-simple-join-in-sql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!