我正在构建一个简单的公交车票预订系统,我面临以下问题:我的MySQL数据库中有两个表destinations
和lines
。这是他们的结构:
+------------------+ +---------------------------------------------------+
| destinations | | lines |
+--+---------------+ +--+-----------+---------+-------------+------------+
|id| name | |id| dest_from | dest_to | depart_time | drive_time |
+--+---------------+ +--+-----------+---------+-------------+------------+
|1 | Boston | |1 | 1 | 2 | 08:00 | 06:00:00 |
|2 | New York City | |2 | 1 | 3 | 08:00 | 04:00:00 |
|3 | Chicago | |3 | 1 | 4 | 08:00 | 04:00:00 |
|4 | Miami | |4 | 2 | 1 | 13:00 | 06:00:00 |
+--+---------------+ |5 | 2 | 3 | 13:00 | 06:00:00 |
|6 | 2 | 4 | 13:00 | 04:00:00 |
|7 | 3 | 1 | 11:00 | 04:00:00 |
|8 | 3 | 2 | 11:00 | 06:00:00 |
|9 | 3 | 4 | 11:00 | 06:00:00 |
|10| 4 | 1 | 09:00 | 04:00:00 |
|11| 4 | 2 | 09:00 | 04:00:00 |
|12| 4 | 3 | 09:00 | 06:00:00 |
+--+-----------+---------+-------------+------------+
因此,正如您所看到的,我使用destinations
的ID,lines
表格dest_from
和dest_to
。
应用程序的界面允许用户选择出发点和到达点,然后点击"搜索"应用程序必须SELECT * FROM lines
,其中dest_from
和dest_to
等于用户选择的目的地。
所以我的问题是:如何选择"捆绑"两个表都有一个SQL查询,所以我可以得到类似Line number 1, from Boston to New York City, departs at 08:00 from Boston and will be in New York City at 14:00.
的东西(08:00 +开车时间06:00小时= 14:00)。
我应该使用JOIN
,如果是,请问查询的外观如何?
提前致谢!
答案 0 :(得分:1)
您可以使用相同的表目标(例如
)创建两个内部联接Fatal Exception: java.lang.SecurityException: Permission Denial: starting Intent { act=android.settings.USAGE_ACCESS_SETTINGS cmp=com.android.settings/.Settings$UsageAccessSettingsActivity } from ProcessRecord{11b5f1a1 19764:com.myproject.my/u0a167} (pid=19764, uid=10167) not exported from uid 1000
at android.os.Parcel.readException(Parcel.java:1546)
at android.os.Parcel.readException(Parcel.java:1499)
at android.app.ActivityManagerProxy.startActivity(ActivityManagerNative.java:2448)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1496)
at android.app.Activity.startActivityForResult(Activity.java:3794)
at android.support.v4.app.BaseFragmentActivityJB.startActivityForResult(BaseFragmentActivityJB.java:48)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:77)
at android.app.Activity.startActivityForResult(Activity.java:3755)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:859)
at com.myproject.my.utils.PermissionsHelper$2.onClick(PermissionsHelper.java:134)
at android.support.v7.app.AlertController$ButtonHandler.handleMessage(AlertController.java:157)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5298)
at java.lang.reflect.Method.invoke(Method.java)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:911)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:706)
答案 1 :(得分:0)
你需要做两个连接,每个连接一个。喜欢这个':
SELECT from.name AS 'from' , to.name AS 'to', depart_time, drive_time
LEFT INNER JOIN destinations to ON dest.id = dest_to
LEFT INNER JOIN destinations from ON dest.id = dest_from
(未经测试的伪SQL,应该给你的想法)