One problem is that they have different named columns
So for example
Table 1
- id
- value_a
- value_b
Table 2
- sid
- value_c
- value_d
I need something like
SELECT id,value_a,value_b AS results FROM table_1 UNION ALL SELECT sid,value_c,value_d AS results FROM table_2 ORDER BY value_a/value_c LIMIT 30
I realize the value_a/value_c isn't what I'd actually do but that's what I mean.
Right now I am able to get the different columns and order alphabetically, I'm just trying to avoid querying again to first figure out which table the current row came from and then grabbing the rest, right now what I've got is
SELECT value_a AS name FROM table_1 UNION ALL SELECT value_c AS name FROM table_2 ORDER BY name LIMIT 30
Can I do what I'm trying to do? I've been going through a bunch of stack overflow threads, not really getting it to work.
edit: this might be what I want
Using union and order by clause in mysql
edit:
The answer I came to from posts here and from other stackoverflow posts:
So you've got your two tables.
To select from both and order by a specific column and also echo out each specific column:
SELECT id AS id, value_a AS value_1, value_b AS value_2, 1 as tblname FROM Table_1 UNION ALL SELECT sid as id, value_c as value_1, value_d as value_2, 2 as tblname FROM Table_2 ORDER BY value_1 LIMIT 30
Then you can write a catch like this:
if ($row['tblname'] === "1") {
// echo out / do stuff to this specific set of columns
echo $row['value_1']; // echos out value_a
}
else if ($row['tblname'] === "2") {
// echo out / do stuff to this specific set of columns
echo $row['value_1']; // echos out value_c
}
Not saying this is the best route, but this single query that gets 30 rows is better than the same command but using more than one query becoming 31 queries (1 query every time you want to get the data knowing what you're looking for in what table)