dongliao3450 2014-04-24 08:29
浏览 48

SQL多个连接,结果为空

I am trying to build a SQL Query for a search request on the table "projects". The search is also related to other Tables that has a relationship to the projects table.

I tried:

SELECT projects.*

FROM projects
  LEFT JOIN documents         ON documents.projectID     = projects.id
  LEFT JOIN subdocuments      ON documents.id            = subdocuments.documentID
  LEFT JOIN subdocuments_tags ON subdocuments.id         = subdocuments_tags.subdocumentID
  JOIN tags                   ON subdocuments_tags.tagID = tags.id

WHERE (projects.name LIKE "%Test%" 
    OR projects.clientName LIKE "%Test%" 
    OR projects.description LIKE "%Test%" 
    OR projects.defaultTags LIKE "%Test%" 
    OR documents.name LIKE "%Test%" 
    OR subdocuments.name LIKE "%Test%" 
    OR documents.description LIKE "%Test%" 
    OR subdocuments.description LIKE "%Test%" 
    OR tags.name LIKE "%Test%")
AND (projects.hidden = 0 
    OR projects.ownerID = 2 
    OR projects_users.userID = 2)

GROUP BY projects.id

ORDER BY projects.updateTime DESC;

The issue is that if the projects don't have any documents and the result is always empty even without a WHERE clause.

  • 写回答

2条回答 默认 最新

  • dptsivmg82908 2014-04-24 09:02
    关注

    @MatBailie

    For further discussion...

     DROP TABLE IF EXISTS i;
    
     DROP TABLE IF EXISTS table_a;
    
     CREATE TABLE ints (i INT NOT NULL PRIMARY KEY);
    
     INSERT INTO ints VALUES (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
    
     CREATE TABLE table_a (i INT NOT NULL,x CHAR(1) NOT NULL, PRIMARY KEY (i,x));
    
     INSERT INTO table_a VALUES
     (1,'a'),
     (1,'b'),
     (1,'c'),
     (1,'d'),
     (1,'e'),
     (2,'a'),
     (2,'b'),
     (2,'c'),
     (3,'a'),
     (3,'b'),
     (4,'a');
    
     SELECT * FROM ints;
     +---+
     | i |
     +---+
     | 0 |
     | 1 |
     | 2 |
     | 3 |
     | 4 |
     | 5 |
     | 6 |
     | 7 |
     | 8 |
     | 9 |
     +---+
    
     SELECT * FROM table_a;
     +---+---+
     | i | x |
     +---+---+
     | 1 | a |
     | 1 | b |
     | 1 | c |
     | 1 | d |
     | 1 | e |
     | 2 | a |
     | 2 | b |
     | 2 | c |
     | 3 | a |
     | 3 | b |
     | 4 | a |
     +---+---+
    
     SELECT m.* FROM ints m LEFT JOIN table_a n ON n.i = m.i WHERE n.x IN('c','d');
     +---+
     | i |
     +---+
     | 1 |
     | 1 |
     | 2 |
     +---+
    
     SELECT m.* FROM ints m JOIN table_a n ON n.i = m.i WHERE n.x IN('c','d');
     +---+
     | i |
     +---+
     | 1 |
     | 1 |
     | 2 |
     +---+
    

    http://www.sqlfiddle.com/#!2/90c6ed/1

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。