duankaolei2921 2012-02-27 03:49
浏览 441
已采纳

MYSQL查询具有不同字段的两个表

Here is my search query, which is putting out an argument error.
What do I need to do to correct it. Basically I need to put both of these tables together and pull out the next 5 events within the two tables by the start date. You will also notice that the last field in each table is different from one another.

$CEsql = mysql_query("
  SELECT id, title, start, end, allDay
  FROM calendar 
  WHERE mem_id='$logOptions_id' 
    AND start >= DATE_SUB( CURDATE( ) ,INTERVAL 0 MONTH ) 

  UNION ALL 

  SELECT id, title, start, end, allDay
  FROM team_calendar 
  WHERE team_id IN ($team_array) 
    AND start >= DATE_SUB( CURDATE( ) ,INTERVAL 0 MONTH ) 
  ORDER BY start ASC LIMIT 5");

I am using MYSQL 5.0 and PHP.

Update

mysql_error displays:

1267: Illegal mix of collations for operation 'UNION'

  • 写回答

2条回答 默认 最新

  • doujieyu7062 2012-02-27 07:52
    关注

    Run SHOW CREATE TABLE statements for both tables. Find differences between two tables, check charsets and collations as Leap Bun suggested. Then you may change field charset/collation, or just try to use COLLATE clause in your SELECT query. Something like this -

    SELECT id, title, start, end, allDay
      FROM calendar 
      WHERE mem_id='$logOptions_id' 
        AND start >= DATE_SUB( CURDATE( ) ,INTERVAL 0 MONTH ) 
    
      UNION ALL 
    
      SELECT id, 
        title COLLATE collation_name AS title, -- write your collation here
        start, end, allDay
      FROM team_calendar 
      WHERE team_id IN (1,2) 
        AND start >= DATE_SUB( CURDATE( ) ,INTERVAL 0 MONTH ) 
      ORDER BY start ASC LIMIT 5
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 求数学坐标画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站