如何编写查询以获取数据并在sql中的特殊记录上跳过4个首字母

我无法从数据库读取数据。有人可以帮我写一个适合我的查询吗?

Select name1,name2 
   if (name2 start with "abcd" or "bcda" or "dasd" then skip 4 first Characters )
From TableName 
where name2 like 'Input'

让我们瞥见一个问题:我想获取name2的数据。类似于“输入”的数据,但是首先我要检查name2是否以“ abcd”开头,那么我不想显示name2的前4个字符。

编辑:我忘了我应该检查2个不同的参数,然后添加或,并且我正在为android开发应用程序并使用SQlite。

我希望我能清楚地解释它,对不起我的英语。

编辑2:我的代码还有问题。它不能很好地显示前4个字母,但问题是它返回这前4个字母。此代码是单词词典的搜索部分。我附上一张照片让你看清楚。 all of these words started with 'das ' and I don't want to show them because I filtered it with below code .

c = mydb.rawQuery("select dId,case when substr(dWord,1,4) in ('das ','der ','die ') then substr(dWord,5,length(dWord) - 4)else dWord end as dWord from german where dWord like '%"+search+"%'",null);
xk0218 回答:如何编写查询以获取数据并在sql中的特殊记录上跳过4个首字母

这是如何编码您所要求的内容。

<android.support.constraint.ConstraintLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="5dp"
    tools:background="@color/quiz_background_tan_color">

    <TextView
        android:id="@+id/your_id"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        android:gravity="start|center_vertical"
        />

</android.support.constraint.ConstraintLayout>
,

如果您的RDMBS支持像regex_replace()这样的正则表达式函数,那就很简单了:

regexp_replace(name2,'^(abcd)|(bcda)','') as name2

这将适用于Oracle,Postgres,MySQL 8.0,PrestoDB,BigQuery,DB2,Sybase(可能还有我现在不考虑的更多功能!)。


在SQLIte中,可以使用字符串函数和大小写表达式:

select 
    name1,case when substr(name2,1,4) in ('abcd','bcda')
        then substr(name2,5,length(name2) - 4) 
        else name2
    end as name2
from mytable
where name2 = 'input'

注意:name2 like 'input'等效于name2 = 'input',因为没有通配符起作用。

本文链接:https://www.f2er.com/3114135.html

大家都在问