问题:
我有2个表我从一个名为Table_Booked的数据中选择数据,其中包含一个记录,每次用户预订时都有一个Username和SessionID,另一个名为Table_Sessions的表包含会话详细信息,如SessionId,Subject,TeacherID,Day, Am_Pm和WhatYear。
我试图从Table_Booked中选择所有记录,其中用户名与当前用户相同,我也希望得到Day&用户已预订的每个会话的Am_Pm数据或他尝试预订的会话的记录。目的是比较记录,如果在同一时间段内发生任何记录,则不允许用户预订会话。
守则:
Select Table_Session.Day,Table_Session.Am_Pm,Table_Session.SessionId
From Table_Session,Table_Booked
Where Table_Booked.Username = 'G9rpenman' and
((Table_Booked.SessionID = Table_Session.SessionId) Or (Table_Session.SessionId = 9))
当我运行此代码时,它从Table_Session.SessionId = 9中获取信息两次,其他调整导致类似的随机数据选择。我找不到任何关于这个的事情。谢谢您的帮助。如果您觉得我错过了任何关键信息,请告诉我并编辑此内容。
显示我的表格的一些屏幕截图答案 0 :(得分:0)
为什么不尝试JOIN语句而不是从两个表中选择你的方式?
以下查询的输出是什么:
package com.example;
import android.content.Context;
import android.util.AttributeSet;
public class EditTextPreference extends android.preference.EditTextPreference
{
public EditTextPreference(Context context, AttributeSet attrs, int defStyle)
{
super(context, attrs, defStyle);
}
public EditTextPreference(Context context, AttributeSet attrs)
{
super(context, attrs);
}
public EditTextPreference(Context context)
{
super(context, null);
}
@Override
protected void onDialogClosed(boolean positiveResult)
{
super.onDialogClosed(positiveResult);
setSummary(getSummary());
}
@Override
public CharSequence getSummary()
{
return getText();
}
}
您是否可以从这两个表中发布一些示例数据,以便我们可以看到您要比较的内容以提供更多帮助?
答案 1 :(得分:0)
简单规则:从不在FROM
子句中使用逗号。 始终使用明确的JOIN
语法。
重复的原因是OR
。我想这会做你想要的:
Select s.Day, s.Am_Pm, s.SessionId
From Table_Booked b join
Table_Session s
on s.SessionID = b.SessionId or s.SessionId = 9
Where b.Username = 'G9rpenman' ;
如果没有,您可能需要union
:
Select s.Day, s.Am_Pm, s.SessionId
From Table_Booked b join
Table_Session s
on s.SessionID = b.SessionId
Where b.Username = 'G9rpenman'
union
Select s.Day, s.Am_Pm, s.SessionId
from Table_Session s
where s.SessionId = 9;