从另一张表中使用ID选择项目

huangapple go评论77阅读模式
英文:

Selecting Items from another table using ID

问题

我有两个表格 Item 和记录每个订单中物品的表格(连接表)

ITEM 表格

String itemTable = "CREATE TABLE " + ITEM_TABLE + " ("
        + ID_ITEM + " INTEGER PRIMARY KEY AUTOINCREMENT,"
        + ITEM_NAME + " TEXT,"
        + ITEM_TYPE + " TEXT,"
        + ITEM_PRICE + " TEXT);";

ORDER_ITEM 表格

String orderItemTable = "CREATE TABLE " + ORDER_ITEM_TABLE + " ("
        + ID_ORDER_ITEM + " INTEGER,"
        + ID_ITEM_ORDER + " INTEGER,"
        + " FOREIGN KEY ("+ID_ORDER_ITEM+") REFERENCES "+ EMP_TABLE +"("+ ID_EMP +"), "
        + " FOREIGN KEY ("+ID_ITEM_ORDER+") REFERENCES "+ EMP_TABLE +"("+ ID_EMP +"));";

ORDER_ITEM 表格中的数据,每个 Item id 都指向物品表格中的特定物品,这是我试图引用并用于从数据库中提取数据的内容。

从另一张表中使用ID选择项目

目前我可以从这个表格中提取 Item id,但不能通过 Id 提取实际的 Item。这里在选择订单时会传递 idOrder

String selctAllEmployeesOrdersItems = "SELECT * FROM " + ORDER_ITEM_TABLE + " WHERE " + ID_ORDER_ITEM + " = " + idOrder;

实现我想要的一种方法是通过在 ArrayList 中存储 Item ID,然后迭代这个列表以选择所有的 Items,但我知道有更好的方法。

我看到的一些研究建议我通过引用 ITEM ID 来连接这些表格,但我不确定正确的语法。点符号在这个查询中不起作用。

String selctAllEmployeesOrdersItems = "SELECT * FROM " + ORDER_ITEM_TABLE + " WHERE " + ID_ORDER_ITEM + " = " + idOrder
        + " JOIN " + ITEM_TABLE + " ON " + ORDER_ITEM_TABLE+"."+ID_ITEM_ORDER + " = " + ITEM_TABLE+"."+ID_ITEM;

显示的错误信息是

Caused by: android.database.sqlite.SQLiteException: near "JOIN":
syntax error (code 1 SQLITE_ERROR): , while compiling: SELECT * FROM
ORDER_ITEM_TABLE WHERE ID_Order = 1 JOIN ITEM_TABLE ON
ORDER_ITEM_TABLE.ID_Item = ITEM_TABLE.ID

英文:

I have two tables Item and a table which records the items in each order (Junction Table)

ITEM Table

String itemTable = "CREATE TABLE " + ITEM_TABLE + " ("
        + ID_ITEM + " INTEGER PRIMARY KEY AUTOINCREMENT,"
        + ITEM_NAME + " TEXT,"
        + ITEM_TYPE + " TEXT,"
        + ITEM_PRICE + " TEXT);";

ORDER_ITEM Table

String orderItemTable = "CREATE TABLE " + ORDER_ITEM_TABLE + " ("
        + ID_ORDER_ITEM + " INTEGER,"
        + ID_ITEM_ORDER + " INTEGER,"
        + " FOREIGN KEY ("+ID_ORDER_ITEM+") REFERENCES "+ EMP_TABLE +"("+ ID_EMP +"), "
        + " FOREIGN KEY ("+ID_ITEM_ORDER+") REFERENCES "+ EMP_TABLE +"("+ ID_EMP +"));";

Data in the ORDER_ITEM table each Item id refers to a specific item in the item table this is what I am trying to refer to and use to pull data from the DB.

从另一张表中使用ID选择项目

At the moment I can pull the Item id from this table but not the actual Item using the Id. Here idOrder is passed when the order is selected

String selctAllEmployeesOrdersItems = "SELECT * FROM " + ORDER_ITEM_TABLE + " WHERE " + ID_ORDER_ITEM + " = " + idOrder;

One way of achieving what I want is by storing the Item ID's in a ArrayList and iterating over this to select all of the Items but I know there is a better way.

Some research I have seen has suggested that I join the tables referencing the ITEM ID but I am not sure of the correct syntax. The dot notation does not work with this query.

String selctAllEmployeesOrdersItems = "SELECT * FROM " + ORDER_ITEM_TABLE + " WHERE " + ID_ORDER_ITEM + " = " + idOrder
        + " JOIN " + ITEM_TABLE + " ON " + ORDER_ITEM_TABLE+"."+ID_ITEM_ORDER + " = " + ITEM_TABLE+"."+ID_ITEM;

The error message being show is

> Caused by: android.database.sqlite.SQLiteException: near "JOIN":
> syntax error (code 1 SQLITE_ERROR): , while compiling: SELECT * FROM
> ORDER_ITEM_TABLE WHERE ID_Order = 1 JOIN ITEM_TABLE ON
> ORDER_ITEM_TABLE.ID_Item = ITEM_TABLE.ID

答案1

得分: 1

你的SQL语句语法有误。<br/>
在连接操作之后,必须编写WHERE子句:

String selctAllEmployeesOrdersItems = 
  "SELECT * FROM " + ORDER_ITEM_TABLE + " AS o " + 
  "INNER JOIN " + ITEM_TABLE + " AS i ON o." + ID_ITEM_ORDER + " = i." + ID_ITEM + " " +
  "WHERE o." + ID_ORDER_ITEM + " = " + idOrder;

请注意上述代码中对两个表的别名oi的使用,显著缩短了代码长度。<br/>

另外,关于表ORDER_ITEM的定义如下:

String orderItemTable = "CREATE TABLE " + ORDER_ITEM_TABLE + " ("
        + ID_ORDER_ITEM + " INTEGER,"
        + ID_ITEM_ORDER + " INTEGER,"
        + " FOREIGN KEY ("+ID_ORDER_ITEM+") REFERENCES "+ EMP_TABLE +"("+ ID_EMP +"), "
        + " FOREIGN KEY ("+ID_ITEM_ORDER+") REFERENCES "+ EMP_TABLE +"("+ ID_EMP +"));";

这似乎不正确。<br/>
EMP_TABLE是什么?<br/>
为什么两个列ID_ORDER_ITEMID_ITEM_ORDER都引用相同的列?<br/>
这没有意义。

英文:

The syntax of your SQL statement is wrong.<br/>
The WHERE clause must be written after the join:

String selctAllEmployeesOrdersItems = 
  &quot;SELECT * FROM &quot; + ORDER_ITEM_TABLE + &quot; AS o &quot; + 
  &quot;INNER JOIN &quot; + ITEM_TABLE + &quot; AS i ON o.&quot; + ID_ITEM_ORDER + &quot; = i.&quot; + ID_ITEM + &quot; &quot; +
  &quot;WHERE o.&quot; + ID_ORDER_ITEM + &quot; = &quot; + idOrder;

Note the use of aliases o and i for the 2 tables that shortens significantly the code.<br/>

Also, the definition of the table ORDER_ITEM:

String orderItemTable = &quot;CREATE TABLE &quot; + ORDER_ITEM_TABLE + &quot; (&quot;
        + ID_ORDER_ITEM + &quot; INTEGER,&quot;
        + ID_ITEM_ORDER + &quot; INTEGER,&quot;
        + &quot; FOREIGN KEY (&quot;+ID_ORDER_ITEM+&quot;) REFERENCES &quot;+ EMP_TABLE +&quot;(&quot;+ ID_EMP +&quot;), &quot;
        + &quot; FOREIGN KEY (&quot;+ID_ITEM_ORDER+&quot;) REFERENCES &quot;+ EMP_TABLE +&quot;(&quot;+ ID_EMP +&quot;));&quot;;

does not seem correct.<br/>
What is the table EMP_TABLE?<br/>
Why do both columns ID_ORDER_ITEM and ID_ITEM_ORDER reference the same column?<br/>
This does not make sense.

答案2

得分: 0

我相信你的子句顺序有误,应该是这样的:
select ... from ... join ... on ... where ...
因此在这个情况下:

SELECT * FROM
ORDER_ITEM_TABLE JOIN ITEM_TABLE
ON ORDER_ITEM_TABLE.ID_Item = ITEM_TABLE.ID
WHERE ORDER_ITEM_TABLE.ID_Order=1;
英文:

I believe you have the clauses in the wrong order, this should be:
select ... from ... join ... on ... where ...
So in this case:

SELECT * FROM
ORDER_ITEM_TABLE JOIN ITEM_TABLE
ON ORDER_ITEM_TABLE.ID_Item = ITEM_TABLE.ID
WHERE ORDER_ITEM_TABLE.ID_Order=1;

答案3

得分: 0

`where` 子句放在连接之后。我还建议使用表别名来缩短查询并使其更易读。最后,您可能只想从物品表中选择列,而不是从连接表中选择列(您已经在连接表上进行了过滤):

SELECT i.*
FROM ORDER_ITEM_TABLE oi
JOIN ITEM_TABLE i ON oi.ID_Item = i.ID
WHERE oi.ID_Order = 1


<details>
<summary>英文:</summary>

The `where` clause goes after the join. I would also recommend using table aliases to shorten the query and make it more readable. Finally, you probably want to select columns from the items table only, not from the junction table (which you are filtering on already):

SELECT i.*
FROM ORDER_ITEM_TABLE oi
JOIN ITEM_TABLE i ON oi.ID_Item = i.ID
WHERE oi.ID_Order = 1

huangapple
  • 本文由 发表于 2020年9月16日 01:51:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/63907361.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定