简体   繁体   English

如何使用sql查询从另一个表中获取数据?

[英]How to get data from another table using sql query?

I have 2 MySQL tables which are below : 我有以下2个MySQL表:

users table ( st_id is station table primary key) 用户表( st_id是工作站表的主键)

uid  fname  lname company_name  email    phone  st_id
=====================================================
9    xxx    yyyy  zzzz          x@y.com  xxx    5,6

station table 站台

st_id  uid   st_name  st_lat  st_long  lg_id
============================================
5      9     xxx      24.25   24.95    8,9,10
6      9     yyy      23.25   23.95    11,12,12

Now using one SQL query I want to get all data from users table and all st_name from station table which st_id is match with uid 现在使用一个 SQL查询,我想从users表中获取所有数据,并从station表中st_id所有st_name ,其中st_iduid匹配

That's mean it's should return all data from users table and all st_name from station table which uid = 9 这意味着它应该返回来自users表的所​​有数据和来自station表的所​​有st_name ,其中uid = 9

My current SQL query : 我当前的SQL查询:

$getData = mysqli_query($conn, "SELECT users.uid, users.company_name, users.fname, users.lname, users.phone, users.email, station.st_id, station.st_name, logger.lg_name FROM users 
LEFT JOIN station ON station.st_id = users.st_id 
LEFT JOIN logger ON logger.lg_id = station.lg_id 
LEFT JOIN channel ON channel.ch_id = logger.ch_id  
WHERE users.power != 'admin' ");

Note: In station table I stored st_id values as array. 注意:station表中,我将st_id值存储为数组。 Like : 喜欢 :

$st_id_value =  implode(',', $st_id_value); 

Update Code : 更新代码:

 $getData = mysqli_query($conn, "SELECT users.uid, users.company_name, users.fname, users.lname, users.phone, users.email, station.st_id, station.st_name, logger.lg_name 
FROM users LEFT JOIN station
ON FIND_IN_SET(station.st_id, users.st_id)
LEFT JOIN logger ON logger.lg_id = station.lg_id 
LEFT JOIN channel ON channel.ch_id = logger.ch_id
WHERE users.power != 'admin' AND station.uid=$uid");

$fetchData = mysqli_fetch_assoc($getData) ;

echo '<pre>';
    print_r($fetchData);
echo '</pre>'; 

Return : 返回:

Array
(
    [uid] => 9
    [company_name] => Source and Services
    [fname] => azad
    [lname] => ahmed
    [phone] => 01671133639
    [email] => azad@gmail.com
    [st_id] => 5
    [st_name] => Rajshahi
    [lg_name] => D4L08841
)

You can try this query. 您可以尝试此查询。

SELECT users.uid, users.company_name, users.fname, users.lname, users.phone, users.email, station.st_id, station.st_name, logger.lg_name 
FROM users INNER JOIN station
ON FIND_IN_SET(station.st_id, users.st_id)
LEFT JOIN logger ON logger.lg_id = station.lg_id 
LEFT JOIN channel ON channel.ch_id = logger.ch_id
WHERE users.power != 'admin' AND users.uid=9

Here you can replace 9 with uid you want. 在这里您可以将9替换为所需的uid

Update 更新资料

$getData = mysqli_query($conn, "SELECT users.uid, users.company_name, users.fname, users.lname, users.phone, users.email, station.st_id, station.st_name, logger.lg_name 
FROM users LEFT JOIN station
ON FIND_IN_SET(station.st_id, users.st_id)
LEFT JOIN logger ON logger.lg_id = station.lg_id 
LEFT JOIN channel ON channel.ch_id = logger.ch_id
WHERE users.power != 'admin' AND station.uid=$uid");

while($fetchData = mysqli_fetch_assoc($getData))
{
    echo '<pre>';
    print_r($fetchData);
    echo '</pre>'; 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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