Re: [PHP] Decide witch table within a union




That seems unreasonably harsh.

I can see what you mean, but don't take it that way. I was trying to help.


What in your view is wrong with a union query that preserves an indicator of which component table a particular record comes from?

Read earlier in the thread. He's talking about adding a field to the table and that the value of this field in every single record will be the name of the table the record belongs to. I said I would definitely not recommend doing that.

I can easily imagine a circumstance in which this could be valuable, say the union of several mailing lists that are in separate tables simply because they originate from different sources. We union them to print a single alphabetical list, for example, but we want an indicator as to the source of each record.

I can imagine modifying the OP's query to read:

$sql=" (select '1' as tableId, * from table_1 WHERE pid='0' order by id desc limit 10) union
(select '2' as tableId, * from table_2 WHERE pid='0' order by id desc limit 10) union
(select '3' as tableId, * from table_3 WHERE pid='0' order by id desc limit 10) union
(select '4' as tableId, * from table_4 WHERE pid='0' order by id desc limit 10)
order by date desc limit 10 ";

Would this be so egregious? and if so why?

I think this is a great solution, wish I'd thought of it. =)


You say,
If you need to combine data from more than one table,
code your application to respond accordingly.

What does this mean, exactly?

I'm just talking about intelligent programming.

Surely you're not suggesting that we code an application to somehow divine the source of a record in a union query when the query itself could simply supply that datum easily.

Of course not. Honestly, I think you're just being critical of what I said, because you thought I was being harsh and unfair when I wasn't actually trying to be.

-Ed
.



Relevant Pages

  • Re: Grouping by Years in Columns
    ... You would first need to normalize your table by using a union query: ... SELECT Hospital, Year, Indicator1 As TheValue, 1 as Indicator ... FROM tblSpreadsheet ... You can then create a crosstab based on the union query that sets the column ...
    (microsoft.public.access.reports)
  • Re: [perfmon2] I.1 - System calls - ioctl
    ... clear indicator it should actually be two sys calls. ... but instead of adding another flag and directly ... passing a union, you also add interface complexity. ...
    (Linux-Kernel)
  • Re: [perfmon2] I.1 - System calls - ioctl
    ... clear indicator it should actually be two sys calls. ... but instead of adding another flag and directly ... passing a union, you also add interface complexity. ...
    (Linux-Kernel)
  • Re: Complex query with different conditions...
    ... but if any of the rows for that product is found with an Indicator is ... I added a primary key OID to ... A.PRODUCT) UNION SELECT OID, PRODUCT, CODE, RATE, INDICATOR FROM ... with your data plus your data with an extra "L" indicator for Product ...
    (comp.databases.ms-access)
  • Re: Combining records from two queries
    ... combining UNION and SELECT queries to best effect. ... The list in each case has to have the same number & types of fields (for example, if the first Query begins with a Date/Time field, the second one should do so as well). ... I like to keep my Union Queries short and simple and do the rest of the work elsewhere. ... tblMentors comprises Subject Mentors and Professional Mentors and the Placement subform has a combo for each - the Subject Mentor combo puts the chosen MentorID in the SubjectMentorID field and the Professional Mentor combo puts the chosen MentorID in the ProfessionalMentorID field. ...
    (microsoft.public.access.queries)