如何从另一个表运行where子句?
例如:
**sentence**
----------
AB
AA
AC
AEF
AHF
,但where条件来自另一个
**text**
sentence like '%A%' and sentence like '%B%'
sentence like '%A%' and sentence like '%C%'
sentence like '%A%' and sentence like '%E%' and sentence like '%F%'
我想要这样的结果:
结果
AB
AC
AEF
我该怎么做? 谢谢
答案 0 :(得分:1)
听起来您需要动态SQL。我建议修复您的数据模型,因此不需要这样做。我认为您可以使用正则表达式来代替存储where
条件:
'A.*B|B.*A'
'A.*C|C.*A'
'A.*E.*F|A.*F.*E|E.*A.*F|E.*F.*A|F.*A.*E|F.*E.*A'
注意:还有其他表达逻辑的方法,但我认为这很清楚。
那么您可以做:
select t1.sentence, t2.pattern
from table1 t1 join
table2 t2
on regexp_contains(t1.sentence, t2,pattern);
答案 1 :(得分:0)
您可以在逻辑条件适当的情况下使用join和concat
Request Method: POST
Request URL: http://127.0.0.1:8000/
Django Version: 2.0.5
Python Version: 3.6.3
Installed Applications:
['django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'account']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware']
Traceback:
File "C:\Users\HP\AppData\Local\Programs\Python\Python36\lib\site-packages\django\core\handlers\exception.py" in inner
35. response = get_response(request)
File "C:\Users\HP\AppData\Local\Programs\Python\Python36\lib\site-packages\django\core\handlers\base.py" in _get_response
128. response = self.process_exception_by_middleware(e, request)
File "C:\Users\HP\AppData\Local\Programs\Python\Python36\lib\site-packages\django\core\handlers\base.py" in _get_response
126. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\HP\Desktop\muvi_2\account\views.py" in SignUpView
10. username = form.cleaned_data['username']
Exception Type: TypeError at /
Exception Value: string indices must be integers
答案 2 :(得分:0)
以下是用于BigQuery标准SQL
#standardSQL
SELECT text, sentence
FROM `project.dataset.table1`,
`project.dataset.table2`,
UNNEST(REGEXP_EXTRACT_ALL(text, "'(.*?)'")) re
GROUP BY sentence, text
HAVING MIN(sentence LIKE re)
如果适用于以下问题中的数据
#standardSQL
WITH `project.dataset.table1` AS (
SELECT 'AB' sentence UNION ALL
SELECT 'AA' UNION ALL
SELECT 'AC' UNION ALL
SELECT 'AEF' UNION ALL
SELECT 'AHF'
), `project.dataset.table2` AS (
SELECT "sentence like '%A%' and sentence like '%B%'" text UNION ALL
SELECT "sentence like '%A%' and sentence like '%C%'" UNION ALL
SELECT "sentence like '%A%' and sentence like '%E%' and sentence like '%F%'"
)
SELECT sentence, text
FROM `project.dataset.table1`,
`project.dataset.table2`,
UNNEST(REGEXP_EXTRACT_ALL(text, "'(.*?)'")) re
GROUP BY sentence, text
HAVING MIN(sentence LIKE re)
结果将是
Row sentence text
1 AB sentence like '%A%' and sentence like '%B%'
2 AEF sentence like '%A%' and sentence like '%E%' and sentence like '%F%'
3 AC sentence like '%A%' and sentence like '%C%'