简体   繁体   English

存在一种从特定表中选择所有数据的简单方法

[英]Is exist a simple way to select all data from specific table

I have two tables: A, B. I need to select all data from B. I could do this like 我有两个表:A,B。我需要从B中选择所有数据。我可以这样做

SELECT id, b1, b2, ... b20 FROM A,B WHERE A.id = B.id; 

it's not cool solution. 这不是很酷的解决方案。 I will need to update this statement if I modify B's database. 如果我修改B的数据库,我将需要更新此语句。 Is exist something like ? 存在类似的东西?

SELECT *(B) 

It selected all data from B and didn't selected any data from A. 它从B中选择了所有数据,但未从A中选择任何数据。

My databases 我的数据库

A
id
a1
a2
...
a20 

B
id
b1
b2
...
b20

Use following query: 使用以下查询:

SELECT * FROM B;

or 要么

SELECT * FROM B INNER JOIN A ON A.id = B.id;

if you want to join tables A and B. 如果你想加入表A和B.

So if you want create database seriously you shouldn't see on complexity of way but on efficiency and speed. 因此,如果你想要认真地创建数据库,你不应该看到复杂的方式,而是效率和速度。 So my advice to you is use JOIN that is the best solution for selecting data from two and more tables, because this way is fast as possible, at least for my more cleaner like for example inserted select . 所以我给你的建议是使用JOIN ,这是从两个或多个表中选择数据的最佳解决方案,因为这种方式尽可能快,至少对于我更干净的方式,例如inserted select

You wrote: I need to select all data from B this means SELECT * FROM B not that you wrote. 你写道: I need to select all data from B这意味着SELECT * FROM B不是你写的。

My advice to your is using this: 我对你的建议是用这个:

SELECT * FROM A <INNER / LEFT / RIGHT / NATURAL> JOIN B ON A.id = B.id;

or to select specific columns 或选择特定列

SELECT A.column1, A.column2, ... FROM A <INNER / LEFT / RIGHT / NATURAL> JOIN B ON A.id = B.id;

Note: 注意:

NATURAL JOIN will work in above example since the primary key and the foreign key in the two tables have the same name. NATURAL JOIN将在上面的示例中起作用,因为两个表中的主键和外键具有相同的名称。 So you must be very careful in using NATURAL JOIN queries in the absence of properly matched columns. 因此,在没有匹配正确的列的情况下,您必须非常小心地使用NATURAL JOIN查询。

So you really should think about how you will create database and how you will working with database, how you will pulling data for View from database, how you will insert new potential data to database etc. 所以你真的应该考虑如何创建数据库以及如何使用数据库,如何从数据库中提取View数据,如何将新的潜在数据插入数据库等。

Regards man! 关心男人!

I suspect that the others have sufficiently answered your question about selecting all fields from table B. That's great, as you really should understand the SQL basics. 我怀疑其他人已经充分回答了关于从表B中选择所有字段的问题。这很好,因为你真的应该理解SQL的基础知识。 If they haven't, I'd also advise that you check out SQLite.org site for a clarification on SQL syntax understood by SQLite. 如果他们没有,我还建议你查看SQLite.org网站,以澄清SQLite理解的SQL语法。

But, assuming you've answered your question, I just want to voice two words of caution about using the asterisk syntax. 但是,假设您已经回答了您的问题,我只想表达关于使用星号语法的两个注意事项。

  1. First, what if, at some later date, you add a column to B that is a big hairy blob (eg a multimegabyte image). 首先,如果在稍后的某个日期,你向B添加一个大毛茸茸的blob(例如,一个多兆字节的图像)。 If you use the * (or B.* ) syntax to retrieve all of the columns from B, you may be retrieving a ton of information you might not need for your particular function. 如果使用* (或B.* )语法从B中检索所有列,则可能会检索大量特定函数可能不需要的信息。 Don't retrieve data from SQLite if you don't need it. 如果您不需要,请不要从SQLite中检索数据。

  2. Second, is your Objective C retrieving the data from your select statement on the basis of the column names of the result, or based upon the index number of the column in question. 其次,您的Objective C是根据结果的列名称或根据相关列的索引号从select语句中检索数据。 If you're doing the latter, then using the * syntax can be dangerous, because you can break your code if the physical order of columns in your table ever changes. 如果您正在使用后者,那么使用*语法可能会很危险,因为如果表中列的物理顺序发生变化,您可能会破坏代码。

Using named columns can solve the problem of memory/performance issues in terms of retrieving too much data, as well as isolating the Objective C from the physical implementation of the table in SQLite. 使用命名列可以在检索过多数据方面解决内存/性能问题,并将Objective C与SQLite中表的物理实现隔离开来。 Generally, I would not advise developers to use the * syntax when retrieving data from a SQL database. 通常,我不建议开发人员在从SQL数据库中检索数据时使用*语法。 Perhaps this isn't an issue for a trivial project, but as projects become more complicated, you may want to think carefully about the implications of the * syntax. 也许这对于一个简单的项目来说不是问题,但随着项目变得更加复杂,您可能需要仔细考虑*语法的含义。

我不知道以下查询是否可以在sqlite中工作,我知道它在Oracle中有效,但是你可以尝试一下......

SELECT B.* FROM A,B WHERE A.id = B.id; 

暂无
暂无

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

相关问题 select 从 BigQuery 中的表中获取几行数据的简单方法? - Simple way to select a few rows of data from table in BigQuery? 如何从一张表中选择关系表中不存在的所有记录? - how to select all records from one table that not exist in relation table? 有没有一种简单的方法可以从具有特定条件的数据表中选择随机记录(大约 20 条) - Is there a simple way to pick random records (about 20) from a data table with specific condition 如何从表中选择数据不存在于另一个表sql中 - How to select data from the table not exist in another table sql 仅当所有需求行均存在时,才从表中选择徽章 - Select badges from table only if all requirements rows exist 有没有办法从表中选择所有字段数据,除了使用nodejs的一个字段数据 - is there any way to select all the fields data from the table except one field data using nodejs MySQL SELECT全部来自第一个表,并使用第二个表过滤器的特定where子句连接来自第二个表的匹配数据 - MySQL SELECT all from first table and join matched data from second table with specific where clause for second table filter 仅按特定行显示所有数据组:从具有column =&#39;value&#39;的列的表组中选择* - show all data only group by specific rows : Select * from table group by column having column = 'value' 有没有办法在SELECT * FROM语句中TRIM所有数据? - Is there a way to TRIM all data in a SELECT * FROM statement? 从具有特定表名的所有表中选择所有值 - Select all values from all tables with specific table name
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM