如何从逗号分隔的数据库字符串中进行SELECT查询 – SQL

huangapple go评论72阅读模式
英文:

How to SELECT from a comma spliced database string - SQL

问题

            String name = null;
            String keyword2 = null;
            String author = "John";
            PreparedStatement stmt3 = DBM.conn.prepareStatement("SELECT * FROM `timelines` LEFT JOIN `users` ON users.UserID = timelines.TimelineOwner WHERE " +
                    " `TimelineName` = COALESCE(NULLIF(?, ''), `TimelineName`) AND FIND_IN_SET(?, `Keywords`) > 0 AND `UserName` = COALESCE(NULLIF(?, ''), `UserName`);");
            stmt3.setString(1, name);
            stmt3.setString(2, keyword2);
            stmt3.setString(3, author);
            List<Timeline> list = DBM.getFromDB(stmt3, new Timeline());

在上面的代码中,我已经根据您的要求修改了查询语句,以便在关键字列比较时将参数与数据库中的单独单词进行比较。这里我使用了MySQL的FIND_IN_SET()函数来实现这一功能。

英文:
            String name = null;
            String keyword2 = null;
            String author = &quot;John&quot;;
            PreparedStatement stmt3 = DBM.conn.prepareStatement(&quot;SELECT * FROM `timelines` LEFT JOIN `users` ON users.UserID = timelines.TimelineOwner WHERE &quot; +
                    &quot; `TimelineName` = COALESCE(NULLIF(?, &#39;&#39;), `TimelineName`) AND `Keywords` = COALESCE(NULLIF(?, &#39;&#39;), `Keywords`) and `UserName` = COALESCE(NULLIF(?, &#39;&#39;), `UserName`);&quot;) ;
            stmt3.setString(1, name);
            stmt3.setString(2, keyword2);
            stmt3.setString(3, author);
            List&lt;Timeline&gt; list = DBM.getFromDB(stmt3, new Timeline());

Here above is my search query for MySQL database and all is fine except there is one addition to it that I would like to add but I am lost in how to code it.

The thing is that the keywords are stored in a comma spliced String in the database and when I change the keyword parameter to, i.e "Rome", it should return that Timeline but it does not because the database String is Rome, Ceasar, Sun...... How can I add to the query so that when it comes to the Keywords column - it should compare the parameter to the words individually?

UPDATE: Maybe I can use FIND_IN_SET() and if the return from that is bigger than 0 then return its a match?

答案1

得分: 1

SELECT * FROM 'timelimes' WHERE 'keywords' LIKE %Rome%

英文:

You can use the SQL LIKE operator to search for a specific keyword in the comma separated string:

SELECT * FROM &#39;timelimes&#39; WHERE &#39;keywords&#39; LIKE %Rome%

答案2

得分: 1

你可以像这样使用FIND_IN_SET()

SELECT * 
FROM `timelines` 
LEFT JOIN `users` ON users.UserID = timelines.TimelineOwner 
WHERE `TimelineName` = COALESCE(NULLIF(?, ''), `TimelineName`) 
  AND FIND_IN_SET(COALESCE(NULLIF(?, ''), SUBSTRING_INDEX(`Keywords`, ',', 1), `Keywords`)
  AND `UserName` = COALESCE(NULLIF(?, ''), `UserName`);

或者使用LIKE操作符:

SELECT * 
FROM `timelines` 
LEFT JOIN `users` ON users.UserID = timelines.TimelineOwner 
WHERE `TimelineName` = COALESCE(NULLIF(?, ''), `TimelineName`) 
  AND CONCAT(',', `Keywords`, ',') LIKE CONCAT('%,', COALESCE(?, '%'), ',')  
  AND `UserName` = COALESCE(NULLIF(?, ''), `UserName`);
英文:

You can use FIND_IN_SET() like this:

SELECT * 
FROM `timelines` 
LEFT JOIN `users` ON users.UserID = timelines.TimelineOwner 
WHERE `TimelineName` = COALESCE(NULLIF(?, &#39;&#39;), `TimelineName`) 
  AND FIND_IN_SET(COALESCE(NULLIF(?, &#39;&#39;), SUBSTRING_INDEX(`Keywords`, &#39;,&#39;, 1), `Keywords`)
  AND `UserName` = COALESCE(NULLIF(?, &#39;&#39;), `UserName`);

or with the operator LIKE:

SELECT * 
FROM `timelines` 
LEFT JOIN `users` ON users.UserID = timelines.TimelineOwner 
WHERE `TimelineName` = COALESCE(NULLIF(?, &#39;&#39;), `TimelineName`) 
  AND CONCAT(&#39;,&#39;, `Keywords`, &#39;,&#39;) LIKE CONCAT(&#39;%,&#39;, COALESCE(?, &#39;%&#39;), &#39;,%&#39;)  
  AND `UserName` = COALESCE(NULLIF(?, &#39;&#39;), `UserName`);

huangapple
  • 本文由 发表于 2020年5月2日 18:30:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/61557859.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定