Problem: How do I concatenate two MySQL tables using two different columns?
I have two MySQL Tables.
**DescriptionTable**. Fields: {filename, ID}.
**ResultsTable**. Fields: {query_id, media_id}. Both fields reference the ID field in the DescriptionTable.
A "match" links a query_id to a specified media_id, and an entry is added into ResultsTable.
I would like it so that I can do a SELECT query that retrieves the following:
[filename (query_id), filename (media_id)]
What I Have Tried:
SELECT a.filename
FROM DescriptionTable a, ResultsTable b
WHERE a.id = b.query_id
... but this only gives me the query_id's filename and not both of the filenames associated with query_id and media_id. How can I incorporate both in one SELECT command?
Join DescriptionTable to ResultsTable twice, once on query_id and once on media_id.
SELECT dq.filename as query_filename, dm.filename as media_filename
FROM ResultsTable r
INNER JOIN DescriptionTable dq ON r.query_id = dq.ID
INNER JOIN DescriptionTable dm on r.media_id = dm.ID
This should return rows with filenames for both query_id and media_id that correspond to the id pairs in the rows of ResultsTable.
Related
I have a little problem with INNER JOIN in mysql query. I have two tables the first is 'kontrola' and 2nd is 'naruszenia' In 'kontrola' table I have rows:
id
podmiot
miasto
wszczeto
zakonczono
naruszenie_id (Foreign KEY on 'naruszenia' table)
In my naruszenia table I have:
id
naruszenia
Now I want to display using INNER JOIN the naruszenie from 'naruszenia' table.
I've create somethin linke this:
$listakontroli = $connecting->query("SELECT * FROM kontrola INNER JOIN naruszenia ON
kontrola.naruszenie_id=naruszenia.id");
But the result is that when I want to display records in table I have changed the ID of first table(kontrola) and the naruszenia_id still showing id from naruszenia table. How to change it to display properly the word not id.
You could use explicit column name and refer to both the table (in this case using k an n) eg:
$listakontroli = $connecting->query("SELECT k.id
, k.podmiot
, k.miasto
, k.wszczeto7
, k.zakonczono
, n.naruszenia
FROM kontrola k
INNER JOIN naruszenia n ON k.naruszenie_id=n.id");
You need to use LEFT OUTER JOIN or separate the ID from the two tables. e.g.
$listakontroli = $connecting->query("SELECT kontrola.id as kid, naruszenia.id as nid, podmiot, miasto, etc* FROM kontrola INNER JOIN naruszenia ON kontrola.naruszenie_id=naruszenia.id");
This way you can properly distinguish the displayed IDs
I have Three Tables in which first two tables have common column to match the record, and 2nd and third table also have common column but there is no direct matching column in first and third table. How shall I write join query ?
Table1(order) Column Names are order_id, patient_id, total, discount
Table2 (order_details) Column Names are order_details_id, order_id, test_id, result
Table3(tests) Column Names are test_id, test_name, test_normal_value
I hope it helps
SELECT * FROM `order`
LEFT JOIN `order_details` ON `order`.`order_id` = `order_details`.`order_id`
LEFT JOIN `tests` ON `order_details`.`test_id` = `test`.`test_id`
"SELECT a.patient_id FROM table1 as a
LEFT JOIN table2 as b on a.order_id = b.order_id
LEFT JOIN table3 as c on c.test_id = b.test_id";
to join this 3 table in one query, we need to assign each table a name. For example table1 AS NewNameTable1 LEFT JOIN table2 AS NewNameTable2. To connecting between this 3 table we need to have a foreign key for each table. So that this 3 table able to JOIN and we able to fetch the data in single query. As long this 3 table is JOINED via foreign key, you can echo out any attribute from any table that Joined.
I have two tables. Both have fields called "ID".
Table 1 has "ID", "Title", "Shift"
Table 2 has "ID", "Table1ID", "Details"
I would like to query Table 2 and retrieve all it's details based on an "ID" value but also get the values from Table 1 that relate to the "Table1ID" value.
I've tried this...
SELECT * FROM Table2 a, Table1 b WHERE a.TableID = b.ID
This works but only retrieves one tables "ID" field.
I've been playing about with UNION ALL but can't get it to work.
Any ideas?
Thanks
Yes, you can add an alias:
SELECT a.ID AID, a.Title, a.Shift, b.ID BID, b.TableID, b.Details
FROM Table2 a, Table1 b
WHERE a.TableID = b.ID
The above will return ID from table A and B as AID and BID in the result.
Is there a way to get the SELECT to get all the fields (without
explicitly writing them) and still alias a specific field?
yes its possible but is considered harmful
This is how you would do id
SELECT Table1.*,Table2.* from table1 inner join Table2 on Table1.ID = Table2.Table1ID;
Why I'm saying its harmful see here : Why is SELECT * considered harmful?
Proper select should be :
SELECT Table1.ID,Table1.Title,Table1.Shift,Table2.ID.Table2.Table1ID,Table2.Details.* from table1 inner join Table2 on Table1.ID = Table2.Table1ID;
I have multiple tables that are linked together on the search page. They are currently queried like this:
SELECT * FROM tripreportentries
JOIN tripreport ON tripreportentries.reportid = tripreport.tripreportid
JOIN user_details on tripreportentries.userid = user_details.userid
WHERE TripEntryHeading LIKE '%%' AND tripreportentries.ispublic = 1
ORDER BY tripreportentryid
I have since created a new table called tripphotos with the table columns named:
tripphotoid
reportid
entryid
photoname
The issue is each entry can have more than one corresponding photo in the photo table.
I am looking to find a way to JOIN the new table to the statement above but not return several rows for each entry id.
Any ideas on how to join this table?
EDIT - I also want to just tie one image to the returned row and not all of them just so I can show the image as a thumbnail on the browse page
I normally would list every column and group by the non aggregates, but mySQL will allow this I believe; given the mySQL group by extensions; and we expect the tripReport and user_Details columns to be the same for each joined photoname.
SELECT tripReport.*, user_details.*, max(photoname) as maxPhotoPerReport
FROM tripreportentries
JOIN tripreport ON tripreportentries.reportid = tripreport.tripreportid
JOIN user_details ON tripreportentries.userid = user_details.userid
JOIN tripphotos ON tripreportentries.reportid = tripPhotos.ReportID
WHERE TripEntryHeading LIKE '%%' AND tripreportentries.ispublic = 1
GROUP BY ReportId
ORDER BY tripreportentryid
or we could do this using an inline view to get the max photo for each report and then join that set to your base query.
SELECT *
FROM tripreportentries
JOIN tripreport ON tripreportentries.reportid = tripreport.tripreportid
JOIN user_details on tripreportentries.userid = user_details.userid
JOIN (SELECT max(photoName) as MaxPhotoPerReport, ReportID
FROM TripPhotos
GROUP BY ReportID) B
ON B.ReportID = tripReport.ReportId
WHERE TripEntryHeading LIKE '%%' AND tripreportentries.ispublic = 1
ORDER BY tripreportentryid
I am trying to select from 2 databases on the same server that have the exact same structure and db2 is a continuance of db1. The problem is I am trying to select the same info "different unique id's though" from both databases and I need the query formatted as such: link_id AS id, title AS title to present the info correctly using $row['title'] etc. Here is my query:
$CONF['mysql_query'] = 'SELECT db1.l.link_id AS id, db1.l.title AS title, db2.r.link_id AS id, db2.r.title AS title FROM db1.links AS l LEFT OUTER JOIN db2.links AS r ON (r.link_id = l.link_id) WHERE l.link_id IN ($ids)';
Why will it not allow me to use title AS title etc for both databases? Is there a way I can do this?
You can't have 2 columns with same name in a SELECT list. That's why you can't have two id or two title.
If you want to join the two tables, use:
$CONF['mysql_query'] = "
SELECT l.link_id AS idL
, l.title AS titleL
, r.link_id AS idR
, r.title AS titleR
FROM db1.links AS l
LEFT OUTER JOIN db2.links AS r
ON (r.link_id = l.link_id)
WHERE l.link_id IN ($ids)
";
The above will show all ids and titles that satisfy the l.link_id IN ($ids) condition from the first table and the corresponding title from the second table. NULLs in the 3rd and 4th column will show if there is no corresponding row in the second table.
If you want a UNION, e.g. all rows from the first table (that satisfy the l.link_id IN ($ids) condition) plus all rows from the second table, use:
$CONF['mysql_query'] = "
SELECT link_id AS id
, title AS title
FROM db1.links AS l
WHERE l.link_id IN ($ids)
UNION
SELECT link_id AS id
, title AS title
FROM db2.links AS r
";
This will possibly show rows with same id, one from each table. Or even duplicate rows. Duplicate rows can be avoided easily though, by putting UNION ALL instead of UNION.
use alias title in single quotes, it will allow you
select t1.Title as 'title' , t2.Title as 'title' from Table1 t1,Table t2