问题
My apologies for wrong terminilogies, I am not actually a programmer, just a Base user confronted with a problem.
I'm having trouble querying a column which is supposed to be a concatenation of two fields of two separate tables. I'm using LibreOffice Base Version 1:3.6.2 and it has the default HSQL engine.
My two tables are as follows:
Table 1 is named "Prefectures"
and has the following fields: ID, "Prefecture Name"
, and "State"
Table 2 is named "Ward"
and has the following fields: ID, "Ward Name"
, and Prefecture ID which is a foreign key referencing table 1.
What I want my query to produce is these two colums: "Ward Name, Prefecture Name" as a concatenation of the Ward.WardName and Prefecture.PrefectureName, and the Ward ID
For example. If I had a prefecture named "Cebu" and a ward named "Lahug" which has a ward ID of 0, I want the query to come up with "Lahug, Cebu" on column 1, and "0" in column 2
The Base tutorial I used seems to have a modified language compared to the actual HSQL language, at least based on my programmer friends reaction. I know that attributes are referred to as Table.Attribute
, for example, but in Base, they use "Attribute"
, or, if I am not mistaken, when one needs to specify the table of origin, "Table"("Attribute")
. However, I know that this modified language works because I used it to create the my two tables.
Anyways, hazarding on what I learned from Base the tutorial document, I came up with:
SELECT "Ward Name" || ', ' || "Prefecture Name" AS "Wrd_Pref",
"Ward ID"
FROM "Prefecture" INNER JOIN "Ward" ON "Prefecture" ("Prefecture ID") = "Ward" ("Prefecture ID")
;
And the error message that came up was: "The data content could not be loaded. Access is denied: PREFECTURE in statement [the whole code above]"
I have a suspicion this is a misread due to wrong syntax on my part. Perhaps my guess on using perentheses in this case is wrong? If so, why is the error message "Access denied"? I checked both the records and the sql codes of the two tables, both were perfectly normal.
EDIT: No, I don't want to just split the two fields. I need them concatenated as I am going to use them as list items in a dropdown list for a form I am making.
回答1:
The SQL query is this one. You say your tables are called "Prefectures" and "Ward", which are used in the FROM clause.
SELECT "Ward Name" || ', ' || "Prefecture Name" AS "Wrd_Pref", "Ward ID"
FROM "Prefectures" INNER JOIN "Ward" ON "Prefectures"."Prefecture ID" = "Ward"."Prefecture ID";
来源:https://stackoverflow.com/questions/16675770/error-in-querying-concatenation-of-2-fields-in-libreoffice-base-sql