为啥这个表我这样写sql不能查出姓名只有两个汉字的学生信息

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了为啥这个表我这样写sql不能查出姓名只有两个汉字的学生信息相关的知识,希望对你有一定的参考价值。

-- 查询姓名为两个汉字的记录
SELECT * FROM stu_student WHERE stu_name LIKE '__';

几个下划线就是几个汉字,这是字符通配符,也可以判断由几个字母组成

参考技术A 你这种写法查出来的是姓名等于“--”的人,从你的表里看是没有的。
你应该这样写where length(s.name) = 4追问

不是一个-可以代表一个字吗

不管怎么查,查出来都为空

本回答被提问者和网友采纳
参考技术B 应该用substring函数
语句中where SUBSTRING(' sname',2,2)
这样应该可以追答

不对,这样错了。你的方式肯定是对的,有可能是哪里打错了吧

参考技术C select * from student where name =“??”;追问

不行

追答

把等号改成like 呢?

追问

name我改为姓名了

引号是什么状态下的,会不会是引号的影响

单引还是双引

追答

不会,你的数据库编码是utf-8吗?

如果作者写了多篇文章,为啥这个 SQL 查询将一篇文章的作者姓名返回为 NULL?

【中文标题】如果作者写了多篇文章,为啥这个 SQL 查询将一篇文章的作者姓名返回为 NULL?【英文标题】:Why is this SQL query returning the author name for an article as NULL, if the author has written more than one article?如果作者写了多篇文章,为什么这个 SQL 查询将一篇文章的作者姓名返回为 NULL? 【发布时间】:2016-12-07 23:21:16 【问题描述】:

这是我的创作声明:

            String createFeedTable = "CREATE TABLE  rss_feed ("
                + "channel_ID INTEGER NOT NULL GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1), "
                + "channelTitle VARCHAR(255), "
                + "channelLink VARCHAR(255), "
                + "channelDescription VARCHAR(255), "
                + "channelPubDate TIMESTAMP, "
                + "channelLastBuildDate TIMESTAMP,"
                + "channelIcon BLOB(1M),"
                + "PRIMARY KEY (channel_ID))";

        String createFeedItemTable = "CREATE TABLE  feed_item ("
                + "  item_ID INTEGER NOT NULL GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1),"
                + "  itemTitle VARCHAR(255),"
                + "  itemLink VARCHAR(255),"
                + "  itemDescription VARCHAR(255),"
                + "  itemGuid VARCHAR(255),"
                + "  itemPubDate TIMESTAMP,"
                + "  channel_ID INTEGER REFERENCES rss_feed(channel_ID),"
                + "  haveRead BOOLEAN DEFAULT FALSE,"
                + "  PRIMARY KEY (item_ID))";

        String createCategoryTable = "CREATE TABLE  category ("
                + "category_ID INTEGER NOT NULL GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1),"
                + "categoryName VARCHAR(255), "
                + "CONSTRAINT category_constraint UNIQUE (categoryName),"
                + "PRIMARY KEY (category_ID))";

        String createFeedItemCategoryTable = "CREATE TABLE  feed_item_category ("
                + "  item_ID INTEGER REFERENCES feed_item(item_ID),"
                + "  category_ID INTEGER REFERENCES category(category_ID))";

        String createFeedCategoryTable = "CREATE TABLE  rss_feed_category ("
                + "feed_ID INTEGER REFERENCES rss_feed(channel_ID),"
                + "category_ID INTEGER REFERENCES category(category_ID))";

        String createAuthorTable = "CREATE TABLE  author ("
                + "author_ID INTEGER NOT NULL GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1),"
                + "authorName VARCHAR(255),"
                + "CONSTRAINT author_constraint UNIQUE (authorName),"
                + "PRIMARY KEY (author_ID))";

        String createFeedItemAuthorTable = "CREATE TABLE  feed_item_author ("
                + "item_ID INTEGER REFERENCES feed_item(item_ID),"
                + "author_ID INTEGER REFERENCES author(author_ID))";

我的查询:

SELECT i.ITEMTITLE, i.ITEMLINK, i.ITEMDESCRIPTION, i.ITEMPUBDATE, i.CHANNEL_ID,
  i.ITEM_ID, a.AUTHOR_ID, a.AUTHORNAME

    FROM FEED_ITEM i
    LEFT JOIN FEED_ITEM_AUTHOR fia
         ON i.ITEM_ID = fia.ITEM_ID
    LEFT JOIN AUTHOR a
         ON fia.ITEM_ID = a.AUTHOR_ID;

结果:

<table border =1>
<tr>  
<th>ITEMTITLE</th>
<th>ITEM_ID</th>
<th>AUTHOR_ID</th>
<th>AUTHORNAME</th>
</tr>
<tr>
<td align="center">Lawyers...</td>
<td align="center">1</td>
<td align="center">1</td>
<td align="center">Joe Mullin</td>
</tr>
<tr>
<td align="center">AT&T/Time Warner...</td>
<td align="center">2</td>
<td align="center">2</td>
<td align="center">Jon Brodkin</td>
</tr>
<tr>
<td align="center">The “technosphere” ...</td>
<td align="center">19</td>
<td align="center">19</td>
<td align="center">Annalee Newitz</td>
</tr>
  <tr>
<td align="center">Decrypted: ...</td>
<td align="center">20</td>
<td align="center">&lt;null&gt;</td>
<td align="center">&lt;null&gt;</td>
</tr>


</table>

feed_item_author 表具有正确的作者和项目数 - 15 个作者和 20 个项目,并且逐行正确匹配数据。

authorfeed_items 表也有正确的行数 - 15 个作者和 20 个 feed_items。

我的代码中也有验证所有内容是否输入正确:

  try 

        feedItem_AuthorStatement = conn.prepareStatement(feed_item_author_SQL, PreparedStatement.RETURN_GENERATED_KEYS);

        feedItem_AuthorStatement.setInt(1, item_ID);
        feedItem_AuthorStatement.setInt(2, author_ID);
        int entrySuccess = feedItem_AuthorStatement.executeUpdate();

        System.out.println("SUCCESS? :" + entrySuccess);
     catch (SQLException ex) 
        Logger.getLogger(FeedItemCategoryHelper.class.getName()).log(Level.SEVERE, null, ex);
    

我在这里缺少什么?如果作者写了不止一篇文章,为什么我的查询返回的作者姓名和 ID 为 null?

【问题讨论】:

【参考方案1】:

您不应该加入 ON fia.AUTHOR_ID = a.AUTHOR_ID;

您希望作者 ID 相同。除非我遗漏了什么,否则希望 itemID 与 authorID 匹配是没有意义的

【讨论】:

宾果游戏。谢谢,Learning2Code。

以上是关于为啥这个表我这样写sql不能查出姓名只有两个汉字的学生信息的主要内容,如果未能解决你的问题,请参考以下文章

用LIKE匹配两个字段,这样一个SQL语言该怎么写?

如果作者写了多篇文章,为啥这个 SQL 查询将一篇文章的作者姓名返回为 NULL?

SQL语句查询姓名包含阿拉伯数字、英文字母等不符合规范的文字怎么写?

sql 一对多的查询

SQL 获取名字拼音

为啥这条sql 只能查出一条数据而且是空的 求大牛指点