473,394 Members | 1,946 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

How to use mySQL union (full join) and order by DATE of just news?

Hello! A rather complicated sql query that I might be makign much more difficult that it should be:
I have two tables:

News:
newsid, datetime, newstext

Picture:
pictureid, datetime, imgPath

Ideally I want a query to return the following kind of table:
Expand|Select|Wrap|Line Numbers
  1. newsid, newstext, numImages, date (date only- no time)
  2. 1       someText   4          2010-01-01
  3. null      null     5          2010-02-01
  4.  
in the results i want a row for every news post with corresponding pictures of they exist- but also a row for the number of images if there was no news.

SQL so far:
Expand|Select|Wrap|Line Numbers
  1. SELECT * FROM news as n LEFT OUTER JOIN (SELECT count(pictureid), datetime
  2. FROM picture GROUP BY DATE(datetime)) as p ON DATE(n.datetime) = DATE(p.datetime)
  3. UNION
  4. SELECT * FROM news as n RIGHT OUTER JOIN (SELECT count(pictureid),
  5. datetime FROM picture GROUP BY DATE(datetime)) as p ON
  6. DATE(n.datetime) = DATE(p.datetime)
  7.  
I have to use union to simulate a full outer join- I want to know when there were pictures even if there was not a post that day.
The results;
Expand|Select|Wrap|Line Numbers
  1. newsid     text     datetime  count()     datetime
  2. 1    sometext   2011-01-16   1          2011-01-16
  3. 2       moo2    2011-01-19  NULL        NULL
  4. 3      mooo3    2011-01-19  NULL        NULL
  5. NULL    NULL      NULL           4      2011-01-14
  6.  
The problem being that I obviously end up with two date columns- one from news and one from pictures, this means i cannot order by date and have it be in the correct order! Any ideas? Even if it means restructuring the database!
Jan 23 '11 #1
2 2546
mwasif
802 Expert 512MB
Does a picture always belong to a news or not?
Jan 23 '11 #2
No it doesn't- the only way pictures are joined is by the date they're added on. They don't belong to any particular news.
Jan 23 '11 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: ++Captain Tripps++ | last post by:
I am trying to run a fuultext search on two identical tables and use UNION to join the results but no matter what I try, I get a syntax error on the UNION clause. Here's the query: (SELECT...
0
by: VisionSet | last post by:
I have a query with 4 tables and plain 'JOIN's the explain gives the best join order, and it completes in 1.5 secs I add a single ORDER BY (a calculated column) and the join orders all shift and...
2
by: Lee | last post by:
I have two identical schema tables (one in a production library and another in a development library) and I need to be able to easily compare the data in them for errors and omissions. With...
1
by: Martijn van Oosterhout | last post by:
Today I got the error: ERROR: FULL JOIN is only supported with mergejoinable join conditions Which is really annoying since a full join is exactly what I wanted. I guess the alternative is to...
7
by: jason.langdale | last post by:
I have 3 tables I want to use in a view. Table A has field 1,2,3,4,5 and table B has field 1,2,3,4,5. I want to do a union on these. (I have done so successfully if I stop here) I also want to join...
2
by: Veeru71 | last post by:
Hi, 1) Is it possible to force the join order of the tables in a query (similar to "hints" in Sybase, etc) ? For Example, I am joining 3 tables - A,B & C in my SELECT stmt, and I would like...
3
by: Smita Kashyap | last post by:
What is the main difference between Full Join and Inner Join in SQl Server 2005 ? As inner join retrive all records that match certain condition, similiary in full join will rreturn all records...
0
by: kabilahan | last post by:
Can any one explain about full join and how it works?
11
by: Israel | last post by:
I've trying to write a query that seems like it should be simple but for some reason my attempts are not working. This is really a general SQL quesion and doesn't pertain to MySQL but I couldn't...
2
by: positivethinking | last post by:
Hello all I am working with oracle 10i I need to join 3 tables in 1 sql statements. (please see attached) it looks simple, but i have spent many hours without any luck, each table have 3...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.