Yes, @Gorets is right. Need a structure of your data. By the names of the tables and * it is impossible to understand what is stored there and what you want to receive in the end in one request.
Suppose that you are interested in all posts and comments on these posts in a single list.
And, say, in the list you need to display a link for the post / comment, the author, the date, maybe the topic ...
Notice that the list of fields that need to be received is formed in advance and all of them are listed in the request, and those fields that are not needed should not be mentioned. If for one table a field from another does not make sense, then instead of this field, write a value, for example, NULL
. It turns out something like:
select 'post' as source, id_post as union_id, author, post_date as _date, title from posts union all select 'quote' as source, id_quote, commenter, comment_date, NULL from quotes order by union_id;
The request will return your list. Depending on the source field, as a result, you will know which table the id
will refer to - posts
or quotes
, because they may coincide ...
NB When using union all
aliases for the sample fields, it is enough to specify for the first table, the union will go in the order of the fields, and not by the names, which is why * in the query can only harm, suddenly the structure changes.
see SQLFiddle for an example of different queries ...
Threat It would be much easier to help if the question was asked more accurately and fully.