親高手看看一個多表查詢的問題

來源:互聯網
上載者:User
關鍵字 php
資料表activity 中有欄位 ac_id ac_title openid 等欄位
資料表 apply 中有ap_id ac_id(既activity表中的ac_id) openid 等欄位

現在需要先查出apply中有 符合使用者的openid的欄位 然後 再查詢出 apply表中ac_id等於activity表中ac_id的資料集

query("select * from activity,apply where apply.ac_id=activity.ac_id order by ap_id desc");

這樣的語句只能查詢出 apply表中ac_id等於activity表中ac_id的資料集 但是不知道怎麼查出 apply中openid等於使用者openid的資料 所以 業務開發的時候不能區分開始那個使用者的 跪求大神提出解決方案 小弟是新手

回複內容:

資料表activity 中有欄位 ac_id ac_title openid 等欄位
資料表 apply 中有ap_id ac_id(既activity表中的ac_id) openid 等欄位

現在需要先查出apply中有 符合使用者的openid的欄位 然後 再查詢出 apply表中ac_id等於activity表中ac_id的資料集

query("select * from activity,apply where apply.ac_id=activity.ac_id order by ap_id desc");

這樣的語句只能查詢出 apply表中ac_id等於activity表中ac_id的資料集 但是不知道怎麼查出 apply中openid等於使用者openid的資料 所以 業務開發的時候不能區分開始那個使用者的 跪求大神提出解決方案 小弟是新手

只要在 sql 中加上 openid 的條件不就行了,就是如下加上 and openid='xxx'

select * from activity,apply where apply.ac_id=activity.ac_id and apply.openid='xxx' order by ap_id desc

假設目前要搜尋的使用者的openid = 2,你的意思是:

1.從資料表activity中搜出openid == 2的資料,作為資料集1.

2.對於資料集1中的資料,在資料表apply中查出apply.ac_id == 資料集1.ac_id 的資料?

左串連怎麼樣親
select * from activity as ac left join apply as ap on ac.ac_id = ap.ac_id where openid = "xxxxx"

  • 相關文章

    聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.