SQL在JOIN查询中选择特定字段
我正在编写一个 Android 应用程序,但遇到了一些涉及数据库的障碍。 Android 处理数据库的方式,我无法通过通常的“tablename.colname”方法引用结果集中的名称,因此当数据库中的任何表包含相同的列名时,这会带来一个巨大的问题。使问题进一步复杂化的是,ViewAdapter 用于向用户显示数据的任何表(如在我的应用程序中)都必须包含一个名为“_id”的字段作为自动增量主键 int。因此,某些表必须具有相同的列名。但是,为了避免这种情况,可以在语句中使用“AS”子句来重命名相关值。但是,我使用的是相当长的语句,并且我不知道如何限制连接表上返回的列。我所拥有的是这个,由于'tablename.colname'引用,它在android中是完全非法的。我实际上添加了表名以使语句更具可读性,但我无法使用它们:
SELECT call._id AS android_call_id,
call.phone,
call.time,
call.duration
call.duration_billed
call.pending
call.call_id
call.job_id
FROM call
LEFT OUTER JOIN phone ON call.phone_number=phone.phone
LEFT OUTER JOIN job ON job._id=call.job_id
WHERE call.pending=1 ORDER BY job._id
但我需要的是使用“AS”语句将 job._id 重命名为其他内容,与“call”相同查询第一部分中的 ._id' 字段。如何在 JOIN 中实现此重命名?
编辑:
到目前为止的进展。我想我已经解决了语法错误,但我收到另一个运行时错误“没有这样的列'job._id',这可能与@Tom H.评论
编辑2有关:
事实证明汤姆是对的,我相应地进行了调整,但它不起作用:
SELECT call._id AS android_call_id,
call.phone,
call.time,
call.duration,
call.duration_billed,
call.pending,
call.call_id,
call.job_id,
job._id AS android_job_id,
job.job_name,
job.job_number
FROM call
LEFT OUTER JOIN phone ON call.phone_number=phone.phone
LEFT OUTER JOIN job ON job._id=call.job_id
WHERE call.pending=1 ORDER BY job._id
错误:
05-24 16:50:37.561: ERROR/Minutemaid - Service(7705): oops: ambiguous column name: call._id: , while compiling: SELECT call._id AS android_call_id,call.phone_number,call.time,call.duration,call.duration_billed,call.pending,call.call_id,call.job_id,job._id AS android_job_id,job.job_name,job.job_number FROM call LEFT OUTER JOIN phone ON call.phone_number=phone.phone LEFT OUTER JOIN call ON call.job_id=job._id WHERE call.pending=1 ORDER BY job._id
i'm writing an Android app and i've run into a bit of a roadblock involving databases. the way Android handles databases, i cannot refer to names in the result set by the usual 'tablename.colname' method, so this presents a huge issue when any tables in the database contain the same column name. what further complicates the issue, is that any table that is used by a ViewAdapter to display the data to the user (as in my application), must contain a field named "_id" as an autoincrement primary key int. therefore, some tables MUST have identical column names. however, to avoid this, it is possible to use an "AS" clause in a statement to rename the value in question. however, i'm using a rather long statement and i don't know how to limit the columns returned on a JOINed table. what i have is this, and it's completely illegal in android due to the 'tablename.colname' references. i actually added the table names in to make the statement more readable, but i can't use them:
SELECT call._id AS android_call_id,
call.phone,
call.time,
call.duration
call.duration_billed
call.pending
call.call_id
call.job_id
FROM call
LEFT OUTER JOIN phone ON call.phone_number=phone.phone
LEFT OUTER JOIN job ON job._id=call.job_id
WHERE call.pending=1 ORDER BY job._id
but what i need, is to rename the job._id to something else using an "AS" statement, same as with the 'call._id' field in the first part of the query. how do i achieve this renaming in a JOIN?
edit:
progress so far. i think i've worked out the syntax errors, but i get another runtime error "no such column 'job._id', which may be related to @Tom H. comment
edit 2:
turns out Tom was right, and i adjusted accordingly, but it doesn't work:
SELECT call._id AS android_call_id,
call.phone,
call.time,
call.duration,
call.duration_billed,
call.pending,
call.call_id,
call.job_id,
job._id AS android_job_id,
job.job_name,
job.job_number
FROM call
LEFT OUTER JOIN phone ON call.phone_number=phone.phone
LEFT OUTER JOIN job ON job._id=call.job_id
WHERE call.pending=1 ORDER BY job._id
error:
05-24 16:50:37.561: ERROR/Minutemaid - Service(7705): oops: ambiguous column name: call._id: , while compiling: SELECT call._id AS android_call_id,call.phone_number,call.time,call.duration,call.duration_billed,call.pending,call.call_id,call.job_id,job._id AS android_job_id,job.job_name,job.job_number FROM call LEFT OUTER JOIN phone ON call.phone_number=phone.phone LEFT OUTER JOIN call ON call.job_id=job._id WHERE call.pending=1 ORDER BY job._id
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
难道您不能简单地使用
AS
将所有tablename.columnname
引用别名为结果集中的唯一名称吗?Can't you simply use
AS
to alias all of thetablename.columnname
references to unique names in the result set?您可以简单地创建一个视图来限制表中可选择的列并为其分配另一个名称。
You can simply create a VIEW that restricts columns selectable in a table and assigns another name to them.
您可以在连接表名称之前尝试通过在 FROM 子句中使用带有 AS 的子查询来调整表名称。例如:
如果限制只是您不能使用表名称来区分列,但可以使用相关名称,那么更简单的是:
You can try massaging the table names before you join them by using sub-queries with AS in the FROM clause. For example:
If the limitation is just that you can't use table names to distinguish between columns but can use correlation names then simpler is: