并从哪里使用mysql加入

时间:2018-11-06 12:08:15

标签: mysql

我需要一些帮助,我是MySQL的新手,刚刚开始学习它并非常了解JOIN

我遇到语法错误:

  

您的SQL语法有错误;

似乎错误出在:AND ( SELECT USERID FROM POSTS WHERE POST_USER = ''在第15行

我的桌子:

===========================================
TABLE USERS 
===========================================
USERID  |   NAME  |  COUNTRY |   STATE
===========================================
2       |   FRED  |  UK      |   LONDON 
3       |   MARK  |  USA     |   CALIFONIA 
4       |   JEFF  |  CA      |   TORONTO 
===========================================

========================================
TABLE POSTS
========================================
POSTID | USERID  | POST_TO  | POST
========================================
2      | 3       | USERID 1   | HEY 
3      | 1       | USERID 3   | SUP 
========================================

我的查询:

    SELECT
  S.USERID,
  S.NAME,
  S.COUNTRY,
  S.STATE,
  P.POST
FROM USERS AS S
INNER JOIN POSTS AS P ON P.USERID = S.USERID AND P.USERID = 1 #THIS SHOULD BE DISPLAYING THE POST_TO NAMES, BUT SHOWING THE POST SENDER NAMES IN HIS ACCOUNT INSTEAD SHOWING RECEIVER NAMES
# ITS LIKE SELECT POST WHERE MY USERID = 1 / DISPLAY NAMES OF THESE PEOPLE I SENT POST TO ON COLUMN POST_TO

UNION

# THIS SECTION IS WORKING OK 
# OR THERE ANYTHING CAN BE DONE ITS OK TO BE CHANGED 
SELECT
  S.USERID,
  S.NAME,
  S.COUNTRY,
  S.STATE,
  P.POST
FROM USERS AS S
INNER JOIN POSTS AS P ON P.USERID = S.USERID AND P.POST_USER = 'USERID 3'


I AM USER 3 - LOGGED IN. I SEE POST FROM USERID 1

I AM USER 1 - LOGGED IN. 
I MUST SEE I SENT POST TO USERID 3 

1 个答案:

答案 0 :(得分:0)

好的,所以我将使用这些数据为您提供示例:

===========================================
TABLE USERS 
===========================================
USERID  | NAME  |  COUNTRY | STATE
===========================================
1       | PAUL  |  FR      | PARIS
2       | FRED  |  UK      | LONDON 
3       | MARK  |  USA     | CALIFONIA 
4       | JEFF  |  CA      | TORONTO 
===========================================

========================================
TABLE POSTS
========================================
POSTID | USERID  | POST_USER  | POST
========================================
2      | 3       | USERID 3   | HEY 
3      | 1       | USERID 1   | SUP 
========================================

要使用两个表中的with列选择所有数据,您可以执行以下操作:

SELECT
  S.USERID,
  S.NAME,
  S.COUNTRY,
  S.STATE,
  P.POST
FROM USERS AS S
LEFT JOIN POSTS AS P ON P.USERID = S.USERID

通过此查询,您将获得:

USERID | NAME | COUNTRY | STATE      | POST
1      | PAUL | FR      | PARIS      | SUP
2      | FRED | UK      | LONDON     | NULL  // POST is NULL because no USERID = 2 in POSTS table
3      | MARK | USA     | CALIFORNIA | HEY  
4      | JEFF | CA      | TORONTO    | NULL  // POST is NULL because no USERID = 4 in POSTS table

现在,如果您只想获得具有POST值的结果,则可以执行以下操作:

SELECT
  S.USERID,
  S.NAME,
  S.COUNTRY,
  S.STATE,
  P.POST
FROM USERS AS S
INNER JOIN POSTS AS P ON P.USERID = S.USERID

结果将是:

USERID | NAME | COUNTRY | STATE      | POST
1      | PAUL | FR      | PARIS      | SUP
3      | MARK | USA     | CALIFORNIA | HEY

这里只有POST不为空的行

现在,如果要以表USER的值之一为条件返回值,则可以执行以下操作:

SELECT
  S.USERID,
  S.NAME,
  S.COUNTRY,
  S.STATE,
  P.POST
FROM USERS AS S
LEFT JOIN POSTS AS P ON P.USERID = S.USERID
WHERE USERID = 1

结果将是:

USERID | NAME | COUNTRY | STATE      | POST
1      | PAUL | FR      | PARIS      | SUP

如果您希望在POSTS表上有条件,则可以像以前一样将INNER JOIN与新条件一起使用:

SELECT
  S.USERID,
  S.NAME,
  S.COUNTRY,
  S.STATE,
  P.POST
FROM USERS AS S
INNER JOIN POSTS AS P ON P.USERID = S.USERID AND P.POST_USER = 'USERID 3'

结果将是:

USERID | NAME | COUNTRY | STATE      | POST
3      | MARK | USA     | CALIFORNIA | HEY

现在,如果要返回条件不同的多行,则可以使用UNION(警告:每个选择必须返回同一列):

SELECT
  S.USERID,
  S.NAME,
  S.COUNTRY,
  S.STATE,
  P.POST
FROM USERS AS S
INNER JOIN POSTS AS P ON P.USERID = S.USERID AND P.USERID = 1

UNION

SELECT
  S.USERID,
  S.NAME,
  S.COUNTRY,
  S.STATE,
  P.POST
FROM USERS AS S
INNER JOIN POSTS AS P ON P.USERID = S.USERID AND P.POST_USER = 'USERID 3'

结果将是:

USERID | NAME | COUNTRY | STATE      | POST
1      | PAUL | FR      | PARIS      | SUP  // Result of first SELECT with P.USERID = 1
3      | MARK | USA     | CALIFORNIA | HEY  // Result of second SELECT with P.POST_USER = 'USERID 3'

是您要找的东西吗?