SQL查询构建

发布于 2024-09-27 22:28:00 字数 1104 浏览 6 评论 0原文

我只是对构建 SQL 查询感到困惑。我尝试成功地分段运行它,但无法将其作为一个整体构建。

情况是这样的:在表 'userseaches' 中,用户 Rushi 已使用不同的 searchID('searchtable' 中的主键) 10, 11, 12 被搜索了 3 次。当我使用 searchID 10 进行搜索时,我得到了 110 个结果,使用 11 进行搜索时,得到了 112 个结果,使用 12 进行搜索时,我得到了 115 个结果。

现在我要做的是获取 ID 11 和 12(最近两次搜索),即 112 和 115 的搜索,并获取上次搜索(112)中新添加的用户的用户名,即三个用户。

我尝试用小块来做,但很难做成完整的。详情请随时询问。

先感谢您。

下面是 3 个表: 表 mastersearches 具有字段:

ID  Name   srchtime

  1  aron   22:10:10

  2  rushi  12:12:14

表 usersearches 具有字段:

ID  masterID  Name  nooffriends
1      2      rushi  110   
2      2      rushi  112
3      2      rushi  115

表 searchtable 具有字段:

ID  searchID Name   friends
1     2       mike    25
2     2       paine   51 
.
112   2       kumar   87
113   3       bandy   23
 .
227   3       ganua    15   

好的。

现在我只想为 searchID 3 获取三个新添加的用户。

现在我尝试了这个(我通过此查询得到了两个搜索 ID):

select
    Name 
from
    usersearches
where
    searchID in (11, 12)

给了所有关注者:

但它无法获取新添加的朋友。

I am just confused in building a SQL query. I tried to run it in pieces successfully but I have not been able to build it as a whole.

This is the case: In table 'userseaches', user Rushi has been searched three times with different searchID's(primary key in 'searchtable') 10, 11, 12 . When I searched with searchID 10 I got 110 results, with 11 got 112 and with 12 I got 115 results.

Now what I have to do is getting searches of ID's 11 and 12(latest two searches) , i.e 112 and 115, and getting the usernames of those users who are newly added in last search(112) i.e three users.

I tried this with small pieces but it is hard to make it in whole. Please feel free to ask for details.

Thank you in advance.

ok below are the 3 tables:
table mastersearches having fields :

ID  Name   srchtime

  1  aron   22:10:10

  2  rushi  12:12:14

table usersearches having fields :

ID  masterID  Name  nooffriends
1      2      rushi  110   
2      2      rushi  112
3      2      rushi  115

table searchtable having fields :

ID  searchID Name   friends
1     2       mike    25
2     2       paine   51 
.
112   2       kumar   87
113   3       bandy   23
 .
227   3       ganua    15   

ok.

Now I want to just fetch three newly added users for the searchID 3.

Now I tried with this (I got two ID's with searches with this query):

select
    Name 
from
    usersearches
where
    searchID in (11, 12)

gave all the followers:

But it is not able to fetch that newly added friends.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夕嗳→ 2024-10-04 22:28:00

我的第一个建议是重新设计你的表结构。您不应在主搜索和用户搜索中同时出现该名称。这导致您必须在两个地方更改名称。查询时的名称应来自一个位置而不是两个位置。我没有看到用户搜索表的目的。它为何存在?我相信它包含可以从 master 和 searchtable 导出的信息。

其次,您不应该在搜索表中手动输入好友数量。这通常会导致额外的统计工作。要么以编程方式分解它,要么将其设置为公式,例如:

SELECT m.*, f.Name as 'Friends_Name', count(s.searchID) as 'Friends'
FROM searchtable f
left join searchtable s
on f.ID = f.Id
left join mastersearches m
on s.searchID = m.ID
group by f.ID

现在,我们需要向 searchtable 添加一个字段来存储朋友输入到表中的时间,我选择了 lastmodified 作为我的字段。

SELECT m.ID, m.Name, f.Name as 'Friends_Name', count(s.searchID) as 'Friends'
FROM searchtable f
left join searchtable s
on f.ID = f.Id
left join mastersearches m
on s.searchID = m.ID
where m.srchtime < s.lastmodified
group by f.ID

如果您需要任何进一步的帮助,请告诉我。
您应该注意,每行都显示“好友”计数,但反映的是主名称而不是可搜索名称。

这是我使用他的模型创建的表的代码。

DROP TABLE IF EXISTS `stack overflow`.`mastersearches`;
CREATE TABLE  `stack overflow`.`mastersearches` (
  `ID` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `Name` text NOT NULL,
  `srchtime` time NOT NULL,
  PRIMARY KEY (`ID`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=latin1;

DROP TABLE IF EXISTS `stack overflow`.`searchtable`;
CREATE TABLE  `stack overflow`.`searchtable` (
  `ID` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `searchID` text NOT NULL,
  `Name` text NOT NULL,
  `friends` int(10) unsigned NOT NULL,
  `lastModified` time NOT NULL,
  PRIMARY KEY (`ID`)
) ENGINE=InnoDB AUTO_INCREMENT=228 DEFAULT CHARSET=latin1;

DROP TABLE IF EXISTS `stack overflow`.`usersearches`;
CREATE TABLE  `stack overflow`.`usersearches` (
  `ID` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `masterID` int(10) unsigned NOT NULL,
  `Name` text NOT NULL,
  `nooffriends` int(10) unsigned NOT NULL,
  PRIMARY KEY (`ID`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=latin1;

My first suggestion, is to redesign your table structure. You should not have the name in both the mastersearches and in the usersearches. This results in you having to change the name in both places. The name when queried should come from one location not both. I am not seeing a purpose for the table usersearches. Why does it exist? It contains information that can be derived from master and searchtable, I believe.

Secondly, you should not have a count friends that you are entering manually in searchtable. This will often result in extra work in tallying this. Either factor it programmaticly or set it as a formula such as:

SELECT m.*, f.Name as 'Friends_Name', count(s.searchID) as 'Friends'
FROM searchtable f
left join searchtable s
on f.ID = f.Id
left join mastersearches m
on s.searchID = m.ID
group by f.ID

Now, we need to add a field to searchtable to store when the friend was entered into the table, I chose lastmodified as my field.

SELECT m.ID, m.Name, f.Name as 'Friends_Name', count(s.searchID) as 'Friends'
FROM searchtable f
left join searchtable s
on f.ID = f.Id
left join mastersearches m
on s.searchID = m.ID
where m.srchtime < s.lastmodified
group by f.ID

If you need any further assistance, please let me know.
You should take note that the count 'Friends' is shown on each row but is reflective of the master name and not the searchtable name.

Here is the code to create the tables I created using his model.

DROP TABLE IF EXISTS `stack overflow`.`mastersearches`;
CREATE TABLE  `stack overflow`.`mastersearches` (
  `ID` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `Name` text NOT NULL,
  `srchtime` time NOT NULL,
  PRIMARY KEY (`ID`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=latin1;

DROP TABLE IF EXISTS `stack overflow`.`searchtable`;
CREATE TABLE  `stack overflow`.`searchtable` (
  `ID` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `searchID` text NOT NULL,
  `Name` text NOT NULL,
  `friends` int(10) unsigned NOT NULL,
  `lastModified` time NOT NULL,
  PRIMARY KEY (`ID`)
) ENGINE=InnoDB AUTO_INCREMENT=228 DEFAULT CHARSET=latin1;

DROP TABLE IF EXISTS `stack overflow`.`usersearches`;
CREATE TABLE  `stack overflow`.`usersearches` (
  `ID` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `masterID` int(10) unsigned NOT NULL,
  `Name` text NOT NULL,
  `nooffriends` int(10) unsigned NOT NULL,
  PRIMARY KEY (`ID`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=latin1;
暗藏城府 2024-10-04 22:28:00

你的问题令人困惑。在您的上下文中,“朋友”和“关注者”是一样的吗?如果是这样,那么尝试这个

Select TOP 2
    Name 
from
    usersearches
where
    searchID in (11, 12) ORDER BY ID DESC

只有当 ID 是连续的时才有效。

另外,您提供了 2 个表“usersearches”和“searchtable”...这些表如何相关?

Your question is confusing. Are "Friends" and "Followers" the same in your context? If so then try this

Select TOP 2
    Name 
from
    usersearches
where
    searchID in (11, 12) ORDER BY ID DESC

This would work only if the ID is sequential.

Also, you provided 2 tables "usersearches" and "searchtable"...how are those tables related?

江挽川 2024-10-04 22:28:00

尝试:

select Name 
from usersearches
where searchID in (11, 12)
group by name
having min(searchID) = 12

Try:

select Name 
from usersearches
where searchID in (11, 12)
group by name
having min(searchID) = 12
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文