基于MySql条件的连接查询

时间:2015-10-26 20:20:22

标签: php mysql

我有两张桌子。这些表有几个匹配的字段。我想用两个条件来获取它们。

首先是a.Show_Link_back=b.Show_Link_back=1,因为这是我们的外键。

第二个是SELECT a.site_id, a.Link_back_Image_URL, a.Link_back_Title, a.Link_back_Description, a.Link_back_URL, a.Show_Link_back, b.Link_back_Image_URL, b.Link_back_Title, b.Link_back_Description, b.Link_back_URL FROM download_site a, product_listing b WHERE a.site_id = b.site_id AND (a.Show_Link_back=1 or b.Show_Link_back=1)

下面是我当前的查询,但是使用此查询我得不到正确的数据,如第一个表(a)没有响应数据。

这是表格的结构

第一个表格字段:

  • SITE_ID
  • Link_back_Image_URL
  • Link_back_Title
  • Show_Link_back

第二个表格字段

  • SITE_ID
  • Link_back_Image_URL
  • Link_back_Title
  • Show_Link_back

我想建立一个关系,即site_id是否为外键,两个表的Show_Link_back值是否为1?

当前查询

CREATE TABLE table1
    (`site_id` int(10), `Link_back_Image_URL` varchar(55), 
     `Link_back_Title` varchar(55),`Link_back_Description` varchar(55),
     `Link_back_URL` varchar(55),`Show_Link_back` int(2));

INSERT INTO table1
    (`site_id`, `Link_back_Image_URL`, `Link_back_Title`,
     `Link_back_Description`,`Link_back_URL`,`Show_Link_back`)
VALUES
    (1, 'test', 'test_title','test_desc','test_url','1'),
    (2, 'test', 'test_title','test_desc','test_url','0'),
    (3, 'test', 'test_title','test_desc','test_url','1');

CREATE TABLE table2
    (`site_id` int(10), `Link_back_Image_URL` varchar(55),
     `Link_back_Title` varchar(55),`Link_back_Description` varchar(55),
     `Link_back_URL` varchar(55),`Show_Link_back` int(2));

INSERT INTO table2
    (`site_id`, `Link_back_Image_URL`, `Link_back_Title`,
     `Link_back_Description`,`Link_back_URL`,`Show_Link_back`)
VALUES
    (1, 'test', 'test_title','test_desc','test_url','1'),
    (2, 'test', 'test_title','test_desc','test_url','0'),
    (3, 'test', 'test_title','test_desc','test_url','1');

这是两个表

的小查询
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:scrollbars="vertical"
    tools:context="com.adroitminds.tabbedview.Section2">


    <EditText
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_marginTop="16dp"
        android:layout_weight="1"
        android:hint="First Name" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_marginTop="16dp"
        android:layout_weight="1"
        android:hint="Last Name" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_marginTop="16dp"
        android:layout_weight="1"
        android:hint="First Name1" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_marginTop="16dp"
        android:layout_weight="1"
        android:hint="Last Name2" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_marginTop="16dp"
        android:layout_weight="1"
        android:hint="Phone2" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_marginTop="16dp"
        android:layout_weight="1"
        android:hint="Email2" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_marginTop="16dp"
        android:layout_weight="1"
        android:hint="Phone" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_marginTop="16dp"
        android:layout_weight="1"
        android:hint="Email" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_marginTop="16dp"
        android:layout_weight="1"
        android:hint="Password" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_marginTop="16dp"
        android:layout_weight="1"
        android:hint="Confirm Password" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="48dp"
        android:layout_weight="1"
        android:layout_marginBottom="16dp"
        android:layout_marginTop="16dp"
        android:text="Submit" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="48dp"
        android:layout_weight="1"
        android:layout_marginBottom="16dp"
        android:layout_marginTop="16dp"
        android:text="Submit1" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="48dp"
        android:layout_weight="1"
        android:layout_marginBottom="16dp"
        android:layout_marginTop="16dp"
        android:text="Submit2" />

</LinearLayout>

1 个答案:

答案 0 :(得分:0)

对我来说很好看。 3行中有2行回来了

SELECT a.site_id,a.Show_Link_back as ASLB,b.Show_Link_back as BSLB,
a.Link_back_Image_URL, a.Link_back_Title,a.Link_back_Description,a.Link_back_URL, b.Link_back_Image_URL,b.Link_back_Title,b.Link_back_Description,b.Link_back_URL
FROM table1 a
join table2 b
on a.site_id=b.site_id
where a.Show_Link_back=1 and b.Show_Link_back=1

+---------+------+------+---------------------+-----------------+-----------------------+---------------+---------------------+-----------------+-----------------------+---------------+
| site_id | ASLB | BSLB | Link_back_Image_URL | Link_back_Title | Link_back_Description | Link_back_URL | Link_back_Image_URL | Link_back_Title | Link_back_Description | Link_back_URL |
+---------+------+------+---------------------+-----------------+-----------------------+---------------+---------------------+-----------------+-----------------------+---------------+
|       1 |    1 |    1 | test                | test_title      | test_desc             | test_url      | test                | test_title      | test_desc             | test_url      |
|       3 |    1 |    1 | test                | test_title      | test_desc             | test_url      | test                | test_title      | test_desc             | test_url      |
+---------+------+------+---------------------+-----------------+-----------------------+---------------+---------------------+-----------------+-----------------------+---------------+

请注意第2列和第3列中使用的别名,以便了解所有这些。