mysql、mssql、oracle 的相同 SQL 连接运算符

发布于 2024-12-08 20:50:36 字数 88 浏览 1 评论 0原文

我试图对上述三个 DBMS 使用相同的 sql 语句..但问题是它涉及字符串连接,但每个 dbms 中有不同的连接操作方式..但我想要单个运算符..需要有人的帮助

I am trying to use same sql statement for the above three DBMS .. but the problem is that it has string concatenation involved but there are different ways in each dbms for concatenation operation .. but i want the single operator .. Need someone's help

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

抽个烟儿 2024-12-15 20:50:36

您也许可以在应用程序代码中解决这个问题,方法是在 sql 语句中使用占位符进行串联,然后将其替换为您正在使用的 RDBMS 的正确样式:

select {conpre} myfield1 {conmid} myfield2 {conmid} myfield3 {conend}
  from mytable

然后在伪代码中:

if rdbms is sqlserver
    conpre = ""
    conmid = " + "
    conend = ""
else if rdbms is mysql
    conpre = "concat("
    conmid = ", "
    conend = ")"
else if rdbms is oracle
    conpre = ""
    conmid = " || "
    conend = ""
else if
    ' etc...
end if

stmt = replace(stmt, "{conpre}", conpre)
stmt = replace(stmt, "{conmid}", conmid)
stmt = replace(stmt, "{conend}", conend)

You can perhaps get around this in your application code by using a placeholder for concatenation in your sql statements, and then replacing it with the correct style for the rdbms you are using:

select {conpre} myfield1 {conmid} myfield2 {conmid} myfield3 {conend}
  from mytable

Then in pseudo-code:

if rdbms is sqlserver
    conpre = ""
    conmid = " + "
    conend = ""
else if rdbms is mysql
    conpre = "concat("
    conmid = ", "
    conend = ")"
else if rdbms is oracle
    conpre = ""
    conmid = " || "
    conend = ""
else if
    ' etc...
end if

stmt = replace(stmt, "{conpre}", conpre)
stmt = replace(stmt, "{conmid}", conmid)
stmt = replace(stmt, "{conend}", conend)
丶情人眼里出诗心の 2024-12-15 20:50:36

我会避免编写自己的问题解决方案,并使用现有的多数据库工具之一。如果您曾经遇到过这个问题,那么您很快就会再次遇到它。

我与以下内容没有任何关系,但您可以尝试 Datanamic Multirun

I'd avoid writing your own solution to the problem and use one of the muti-database tools already available. If you have come across this problem once you will come across it again soon.

I've no affiliation with the following but you could try Datanamic Multirun

小忆控 2024-12-15 20:50:36

这个问题的简单答案似乎是否定的。

但是...

如果您在 Oracle 中创建包 dbo 会怎样?
在 mysql 中是否也可以在名为 dbo 的单独数据库中创建一个名为 concat 的函数,以便使用语法 dbo.concat(a, b, c) 调用函数?

不幸的是,mysql不支持默认参数(除非最近更改)或函数重载,因此您必须为每个参数数量创建函数:

concat2(s1, s2)

concat3(s1, s2, s3)

等等。

The simple answer is to the question seems to be no.

However...

What if you create the package dbo in Oracle?
Is it not also possible in mysql to create a function called concat in a separate database called dbo, so that a function is called using the syntax dbo.concat(a, b, c)?

Unfortunately, mysql doesn't support default parameters(unless recently changed) or function overloading, so you would have to create on function for each number of arguments:

concat2(s1, s2)

concat3(s1, s2, s3)

and so on.

杀手六號 2024-12-15 20:50:36

有一种方法可以使用 ODBC 转义序列来实现此目的

SELECT {fn concat (col1, {fn concat (col2, col3)})}
FROM YourTable

我目前的理解 这在 SQL Server 和 MySQL 中可以正常工作,但对于 Oracle 来说取决于连接方法。

There is a way of doing this using ODBC escape sequences

SELECT {fn concat (col1, {fn concat (col2, col3)})}
FROM YourTable

From my current understanding this will work fine in SQL Server and MySQL but for Oracle is dependant upon connection method.

墨小沫ゞ 2024-12-15 20:50:36

MySQL:

   SELECT CONCAT('New ', 'York ', 'City');

输出为:纽约市

Oracle:

SELECT 'The city' || ' is ' || 'Paris' FROM dual;

输出为:城市为巴黎

SQL Server:

SELECT 'The city' + ' is ' + 'Paris';

输出为:城市为巴黎

 SELECT CONCAT('The city', ' is ', 'Paris');

输出为: 城市是巴黎

MySQL:

   SELECT CONCAT('New ', 'York ', 'City');

Output is : New York City

Oracle:

SELECT 'The city' || ' is ' || 'Paris' FROM dual;

Output is : The city is Paris

SQL Server:

SELECT 'The city' + ' is ' + 'Paris';

Output is : The city is Paris

 SELECT CONCAT('The city', ' is ', 'Paris');

Output is : The city is Paris

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文