简体   繁体   English

我有2个MySql表。 如何在1 sql查询中获取不是所有数据?

[英]I have 2 MySql tables. How to get not all data from them in 1 sql query?

I have 2 MySql tables. 我有2个MySql表。 users with id's and username's ; 具有id和用户名的用户; streams with userId's and streamId's How to get them as / join them into one table containing only username | streamId 使用userId和streamId的流如何将它们作为/加入到一个只包含username | streamId表中 username | streamId as SQL response? username | streamId作为SQL响应? With one SQL query. 使用一个SQL查询。

you can do the following: 你可以做到以下几点:

select a.username, b.streamId
from names a, streams b
where a.userId = b.userId;
select tb1.username, tb2.streamid 
from tb1
inner join tb2 on tb2.userid = tb1.userid

The response above returns the same results, just contains an implicit join which my be slower. 上面的响应返回相同的结果,只包含一个隐式连接,我的速度较慢。

select u.username, max(s.streamId) as streamId
from users u
inner join streams s on u.id = s.userId
group by u.username

暂无
暂无

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

相关问题 在三个表的查询中。 如何在 SQL 中获得一组团队及其成员 - In a query of three tables. How to get an array of teams with their members in SQL 从动态文本字段将数据插入mysql,然后将数据插入两个表中。 - insert data into mysql from the dynamic text field and have ti insert the data into two tables. 从 mysql 表中获取数据并在 sql 查询中的“IN”中使用它们 (PHP) - Get data from mysql table and use them in "IN" in sql query (PHP) 我有 3 个有关系的表。 第一个表具有另一个两个表的外部 id。 我想知道获取数据的确切查询 - I have 3 tables with relation. The first table have foreign id of another both tables. I want to know the exact query of fetching the data 获取所有表的名称。 教义+ Codeigniter - Get names of all tables. Doctrine + Codeigniter mysql从两个不同的表中选择。 - mysql Selecting from two different tables. 如何选择数据库的所有表mySql并按数据显示查询顺序,表具有相同的结构 - How to SELECT all the tables mySql of a db and show query order by data, tables have the same structure 如何从涉及两个表的查询中获取单行。 子表的一项中重复使用2个外键 - How to get single row from a query involving two tables. 2 Foreign Keys are repeated in one entry of child table 我如何使用mysql中的单个查询从两个表中获取数据 - How do i get data from two tables using single query in mysql 如何通过一个查询从3个不同的mysql表中获取数据 - How to get data from 3 different mysql tables with one query
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM