gpt4 book ai didi

java - 使用内连接将多个 sql 查询转为单个查询

转载 作者:行者123 更新时间:2023-12-02 07:43:31 27 4
gpt4 key购买 nike

我有这个java代码:

ArrayList<MessageMap> ids = getNewMail(UserID, type, maxIdMessage);
Message[] message = new Message[ids.size()];
if(ids.size()>0){
ResultSet rs;
int j =0;
for(MappaMessaggi i : ids){
pstmt = conn.prepareStatement("SELECT * FROM Messaggi WHERE MessageID = ?");
pstmt.setInt(1, i.getMessageID());
rs = pstmt.executeQuery();
rs.next();
.
.
.
}

其中 getNewMail 是:

public synchronized ArrayList<MessageMap> getNewMail(int UserID,int type,int max){
ArrayList<MessageMap> map = new ArrayList<MessageMap>();
try {
pstmt = conn.prepareStatement("SELECT * FROM MessageMap WHERE UserID = ? AND TipoID = ? AND MessageID > ?");
.
.//fill arraylist with resultSet
.
}

我知道只需一个查询就可以做同样的事情,但我不知道如何..有人可以睁开我的眼睛吗? :)谢谢!!!

编辑:我尝试:

SELECT i.*
FROM Messaggi AS i
INNER JOIN MessageMap AS p i.MessageID = p.MessageID
WHERE p.MessageID = 1 AND p.UserID = 1 AND p.TipoID = 2

但我检索到:

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'i.MessageID = p.MessageID WHERE p.MessageID = 1 AND p.UserID = 1 AND p.TipoID = ' at line 3

最佳答案

试试这个:

SELECT i.*, p.*
FROM Messaggi i
INNER JOIN MessageMap p ON i.MessageID = p.MessageID
WHERE i.MessageID = ? AND p.UserID = ? AND p.TipoID = ?

关于java - 使用内连接将多个 sql 查询转为单个查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11247455/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com