寻求MySQL与HSQLDB通用的UUID获取SQL语句方案
Ah, the classic multi-database syntax headache! You're right that MySQL lets you run SELECT UUID(); directly, while HSQLDB requires a FROM clause (or workarounds like VALUES/CALL). Here's how to write a query that works seamlessly in both:
The Simplest Solution: Use the DUAL Dummy Table
Both databases support the DUAL virtual table, which is perfect for queries that don't need a real underlying table:
SELECT UUID() FROM DUAL;
- MySQL:
DUALis a built-in dummy table, so this behaves exactly like runningSELECT UUID();directly. - HSQLDB:
DUALis recognized as a valid virtual table, satisfying its requirement for aFROMclause while properly executing theUUID()function.
Alternative: Use a VALUES Subquery
If you prefer avoiding DUAL, the VALUES subquery you already tested works in both databases too:
SELECT UUID() FROM (VALUES(0)) AS t;
MySQL handles this subquery as a valid data source, and HSQLDB already accepts this syntax as you've verified.
Quick Note
Just ensure HSQLDB's UUID function is enabled (you mentioned this is already confirmed, which is great—this feature is enabled by default, but it's worth double-checking if you run into unexpected issues).
内容的提问来源于stack exchange,提问作者user2083529

