mysql select查询从同一用户名表调用名称的多个ID
快问。感觉自己像个菜鸟,但还没有找到正确的语法。
有3张桌子。
T1- 评估ID(自动递增) 检查员ID, 助理ID, 植物名称
T2- 用户ID(自动递增), 用户名, 名称
T3- 植物ID(自动递增) PlantName
基本上我想要一个与 T1 非常相似的 php/mysql 表,但我不想列出 ID,而是想要名称。到目前为止,这是我的调用:
SELECT DISTINCT a.AssessmentID, u.Name, a.PlantAssistID, p.PlantName
FROM assessmentscores AS a, user AS u, plant AS p
WHERE u.userID=a.InspectorID AND u.Name='$name' AND p.PlantID=a.Plant
这有效,因为 u.Name 将为我提供检查员的名称,但我不知道如何调用第二个(a.PlantAssistID)。只要继续为两者取相同的名字即可。
我应该使用什么调用服务器来返回两个不同的名称。任何帮助将不胜感激
Quick question. feel like a noob but haven't found the right syntax for this yet.
Have 3 tables.
T1-
AsessessmentID(auto Incr)
inspectorID,
assistantID,
plant name
T2-
UserID(auto Incr),
username,
name
T3-
plantID(auto Incr)
PlantName
Basically I want to have a php/mysql table that is very similar to T1 but instead of listing the ID's I would like the names. Heres my call so far:
SELECT DISTINCT a.AssessmentID, u.Name, a.PlantAssistID, p.PlantName
FROM assessmentscores AS a, user AS u, plant AS p
WHERE u.userID=a.InspectorID AND u.Name='$name' AND p.PlantID=a.Plant
This works as u.Name will give me the name for the inspector but I can't figure out how to call the second (a.PlantAssistID). Just keep getting the same name for both.
What call to the server should I use to return two different names. Any help would be appreciated
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先要做的事情是:如果您想从评估表中进行选择并使用其他表扩充结果集,请考虑使用 LEFT JOIN 。如果您想要更严格的结果(仅返回与现有用户实际关联的评估),请改用
INNER JOIN
。下面的示例使用LEFT JOIN
。不确定这是否正是您想要的,但您可以尝试:
结果:
First things first: if you want to select from the assessment table and augment the resultset using the other tables, think about using
LEFT JOIN
. If you want a more restrictive result, where only assessments are returned that are actually associated with existing users, use anINNER JOIN
instead. The example below usesLEFT JOIN
.Not sure whether this is what you want exactly, but you can try:
Result:
我不是 100% 确定我明白你的意思,但这就是我认为你想要的:
我会重写查询以使用 JOIN,因为它可以更容易阅读
这里的关键是重新加入用户表,但给它一个不同的别名(此处为 u2)
I'm not 100% sure I follow you, but here's what I think you want:
I would rewrite the query to use JOIN, as the can be easier to read
The key here is to rejoin on the user table but to give it a different alias (u2 here)