使用 SQL 查找访问过 URL A 和 B 的用户?

问题描述

假设您有一个包含 user,url,datetime 的表格,其中每一行代表一次网站访问。

如何查找既访问过包含字符串模式 A 的 URL 又访问包含字符串模式 B 的 URL 的用户

事实上它“包含一个字符串模式......”,而不是一个简单的相等性使得不可能使用类似的查询

url in ('action1.PHP','action2.PHP')

就像在SQL querying a customer ID who ordered both product A and B中一样。

解决方法

您可以使用 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <form> <h4>Q1:</h4> <label>answer1</label> <input type="radio" name="q1" value="answer1" data-id="1" class="answer"> <label>answer2</label> <input type="radio" name="q1" value="answer2" data-id="2" class="answer"> <label>answer3</label> <input type="radio" name="q1" value="answer3" data-id="3" class="answer"> <h4>Q2:</h4> <label>answer1</label> <input type="radio" name="q2" value="answer1" data-id="4" class="answer"> <label>answer2</label> <input type="radio" name="q2" value="answer2" data-id="5" class="answer"> <label>answer3</label> <input type="radio" name="q2" value="answer3" data-id="6" class="answer" checked> <div> <input type="submit" value="Submit"> </div> </form>group by

having

如果不想重复比较,可以省略 select user from t where url like '%a%' or url like '%b%' group by user having sum(url like '%a%') > 0 and sum(url like '%b%') > 0; 子句或使用:

where
,

假设“/testing”和“/staging”是两种 URL 模式。你可以用这个

package com.example.newsapp

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.newsapp.databinding.ActivityMainBinding
import kotlinx.coroutines.*
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import java.lang.Exception

class MainActivity : AppCompatActivity() {

    private lateinit var binding: ActivityMainBinding
    private lateinit var adapter: RecyclerAdapter

    private val newsTitles = mutableListOf<String>()
    private val newsImages = mutableListOf<String>()
    private val newsAuthors = mutableListOf<String>()
    private val newsDescriptions = mutableListOf<String>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)
        makeAPIRequest()
    }

    private fun initRecyclerView() {
        adapter = RecyclerAdapter( newsTitles,newsImages,newsAuthors,newsDescriptions)
        binding.rvNews.layoutManager = LinearLayoutManager(this)
        binding.rvNews.adapter = adapter
    }


    private fun addToList(title:String,image:String,author:String,description:String){
        newsTitles.add(title)
        newsImages.add(image)
        newsAuthors.add(author)
        newsDescriptions.add(description)
    }

    private fun makeAPIRequest() {

        val api = Retrofit.Builder()
                .baseUrl("https://newsapi.org/")
                .addConverterFactory(GsonConverterFactory.create())
                .build()
                .create(APIService::class.java)

        GlobalScope.launch(Dispatchers.IO){
            val response = api.getNews()
            val posts = response.body()
            try{
                if (posts != null) {
                    for(art in posts.Articles){
                        Log.i("Main Activity","Result = $art")
                        addToList(art.Title,art.urlToImage,art.Author,art.Description)
                    }
                }
                    withContext(Dispatchers.Main){
                        initRecyclerView()
                    }
                } catch (e:Exception){
                    Log.e("Main Activity",e.toString())
                }
        }
    }

}

如果您需要有关模式匹配的更多信息,可以搜索“模式匹配 SQL”和“SQL 正则表达式”。