我有五个查询,每个查询都会返回单列多行输出。我想编写一个包含所有这些查询的函数 有人可以帮忙吗?
查询1:
X, Y
查询2:
public class Aptitude extends Activity implements OnClickListener {
DBHelper helper;
SQLiteDatabase db;
Button btnBegin,btnnext;
//Sub category Buttons
Button btnaptitude5,btnaptitude4,btnaptitude3,btnaptitude2,btnaptitude1;
String Question_ID,Title,TitleDescription,QuestionText,QuestionTemplate,QuestionImage;
TextView tvTitle,tvInstructions,tvQuestionText;
RadioGroup rgtemplate4images;
ImageView img;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.aptitude);
// these buttons are on a different layout xml file...
btnaptitude1 = (Button)findViewById(R.id.btnaptitude5);
btnaptitude2 = (Button)findViewById(R.id.btnaptitude3);
btnaptitude3 = (Button)findViewById(R.id.btnaptitude1);
btnaptitude4 = (Button)findViewById(R.id.btnaptitude2);
btnaptitude5 = (Button)findViewById(R.id.btnaptitude4);
btnaptitude1.setOnClickListener(this);
btnaptitude2.setOnClickListener(this);
btnaptitude3.setOnClickListener(this);
btnaptitude4.setOnClickListener(this);
btnaptitude5.setOnClickListener(this);
helper=new DBHelper(this);
btnBegin = (Button)findViewById(R.id.btnBeginAptitude);
btnBegin.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (v==btnBegin)
{
setContentView(R.layout.aptitudesubcategory);
if (v== btnaptitude1)
{
getAllQuestionsAptitude("1","1");
}
// setContentView(R.layout.template4optionsimage);
// tvTitle = (TextView)findViewById(R.id.tvTitleTemplate4optoinsImage);
// tvInstructions = (TextView)findViewById(R.id.tvInstructionstemplate4);
// tvQuestionText = (TextView)findViewById(R.id.tvQuestionTextTemplate4Image);
//
// img = (ImageView)findViewById(R.id.imageViewtemplate4options);
//
// tvTitle.setText(Title);
// tvInstructions.setText(TitleDescription);
// tvQuestionText.setText(QuestionText);
//
// String imgName = QuestionImage; // specify here your image name fetched from db
// String uri = "drawable/" + imgName;
// int icon = getResources().getIdentifier(uri, "drawable", getPackageName());
// img.setImageResource(icon);
}
}
}
从一个函数我想得到一个将有列的表
07-16 06:47:18.931: E/AndroidRuntime(13440): FATAL EXCEPTION: main
07-16 06:47:18.931: E/AndroidRuntime(13440): Process: com.cldonline.assesmenttool, PID: 13440
07-16 06:47:18.931: E/AndroidRuntime(13440): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.cldonline.assesmenttool/com.cldonline.assesmenttool.Aptitude}: java.lang.NullPointerException
07-16 06:47:18.931: E/AndroidRuntime(13440): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
07-16 06:47:18.931: E/AndroidRuntime(13440): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
07-16 06:47:18.931: E/AndroidRuntime(13440): at android.app.ActivityThread.access$800(ActivityThread.java:135)
07-16 06:47:18.931: E/AndroidRuntime(13440): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
07-16 06:47:18.931: E/AndroidRuntime(13440): at android.os.Handler.dispatchMessage(Handler.java:102)
07-16 06:47:18.931: E/AndroidRuntime(13440): at android.os.Looper.loop(Looper.java:136)
07-16 06:47:18.931: E/AndroidRuntime(13440): at android.app.ActivityThread.main(ActivityThread.java:5017)
07-16 06:47:18.931: E/AndroidRuntime(13440): at java.lang.reflect.Method.invokeNative(Native Method)
07-16 06:47:18.931: E/AndroidRuntime(13440): at java.lang.reflect.Method.invoke(Method.java:515)
07-16 06:47:18.931: E/AndroidRuntime(13440): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
07-16 06:47:18.931: E/AndroidRuntime(13440): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
07-16 06:47:18.931: E/AndroidRuntime(13440): at dalvik.system.NativeStart.main(Native Method)
07-16 06:47:18.931: E/AndroidRuntime(13440): Caused by: java.lang.NullPointerException
07-16 06:47:18.931: E/AndroidRuntime(13440): at com.cldonline.assesmenttool.Aptitude.onCreate(Aptitude.java:56)
07-16 06:47:18.931: E/AndroidRuntime(13440): at android.app.Activity.performCreate(Activity.java:5231)
07-16 06:47:18.931: E/AndroidRuntime(13440): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
07-16 06:47:18.931: E/AndroidRuntime(13440): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
07-16 06:47:18.931: E/AndroidRuntime(13440): ... 11 more
如何在单一功能下加入这些查询?
答案 0 :(得分:1)
使用UNION运算符:
SELECT
column_1
FROM
tbl_name_1
UNION ALL
SELECT
column_1
FROM
tbl_name_2;
答案 1 :(得分:1)
为每个查询添加ROW_NUMBER()
并按行号加入。
根据您加入的每个查询返回的行数,然后inner
,left
或full
加入。
下面的示例假定两个查询返回相同的行数。
WITH
CTE1
AS
(
SELECT Col1 as X, ROW_NUMBER() OVER(ORDER BY Col1) AS rn
FROM Table1
)
,CTE2
AS
(
SELECT Col3 as Y, ROW_NUMBER() OVER(ORDER BY Col3) AS rn
FROM Table2
)
SELECT
CTE1.X, CTE2.Y
FROM
CTE1
INNER JOIN CTE2 ON CTE1.rn = CTE2.rn
答案 2 :(得分:0)
如果两个表之间存在关联,请尝试使用join。
也许这里有一个简单的inner join
?
select Col1 as X from Table1
join
on Table1.Col1_name = Table2.col3_name