MySQL Inner Join 2 tables - php

SELECT *
FROM tableA a
INNER JOIN tableB b
ON a.someColumn = b.otherColumn
INNER JOIN tableC c
ON b.anotherColumn = c.nextColumn
What if tableA and tableB has the same name in fields? How can I use:
<?
$name = mysql_fetch...
echo $name['a.title'] . ' ' . $name['b.title'];
?>
So I could get title from tableA and title from tableB. Because now, if use just $name['title'] it returns tableA's title. And the above code unfortunately gives just an empty string.

Instead of doing select *, you can put an alias on the columns as well.
SELECT a.title AS 'a_title', b.title AS 'b_title'
-- ...
Then your PHP would be something like:
$name['a_title']

Use an alias
SELECT a.title AS TitleA,
b.title AS TitleB,
...
FROM ...
Then reference the alias:
$name['TitleA']

Stop using SELECT *!
Explicitly list your columns, which will then allow you to alias them as needed.
SELECT a.title AS TableATitle, b.title AS TableBTitle, ...

Instead of using SELECT *, you will have to call out the fields by name. When you do this, you can assign an alias to be used for each.
SELECT a.title AS aTitle, b.title AS bTitle, etc...

SELECT *, a.title as atitle, b.title as btitle
FROM tableA a
INNER JOIN tableB b
ON a.someColumn = b.otherColumn
INNER JOIN tableC c
ON b.anotherColumn = c.nextColumn

Related

compare two tables with same column for similar values

I have two tables tableA and tableB. Both have two similar columns ID and B_ID.
I want to check whether both table have similar values. My code is:
$ac = $mysql->query("(SELECT ID,B_ID FROM tableA) INTERSECT (SELECT ID,B_ID FROM tableB)");
if($ac){
while($row = $ac->fetch_assoc()){
echo "ID ".$row["ID"]." B_ID".$row["B_ID"]."<br>";
}
}
But this doesn't give any result.
ps: tableA(ID,B_ID)
1->23
2->23
3->23
4->56
5->67
tableB(ID,B_ID)
3->23
8->26
11->27
12->66
here both table has 3->23 but 1->23 2->23 is not in tableB how can i figure that sort of records. same B_ID but different ID
If you have B.ID column is present in both table then use following JOIN query
SELECT a.ID, a.B_ID
FROM tableA AS a
JOIN tableB AS b ON (a.B_ID = b.B_ID AND a.ID = b.ID)
Use a join to get the data and just iterate over your results. Run the query and if there is any record that satisfies the query or not.
select t1.ID, t1.B_ID from tableA t1, tableB t2
where t1.ID = t2.ID
and t1.B_ID =t2.B_ID
Use an INNER JOIN and COUNT
SELECT COUNT(*) as cnt
FROM tableA INNER JOIN tableB
ON tableA.B_ID = tableB.B_ID AND tableA.ID = tableB.ID
now if cnt is greater than zero than common values exist, otherwise no
Please try with this query may be help full.
SELECT
tbla.ID,
tbla.B_ID
FROM
tablea AS tbla,
tableb AS tblb
WHERE
tbla.B_ID = tblb.B_ID

Output from two tables that has two same name columns

I do have a MySQL script that put together two tables using INNER JOIN. Both tables, have a field called ID.
I do need to output with PHP, both ID fields. How do I do this?
This is my script:
$sql = "SELECT a.id,
a.route_id,
a.requester,
a.reservation,
a.reservation_date,
a.reservation_by,
a.telephone,
a.email,
a.firstname,
a.lastname,
a.qty_pax,
a.date_trip,
a.time_trip,
a.trip_type,
a.cancelled,
a.notes,
a.room,
a.driver_id,
b.id,
b.dep_symbol,
b.dep_location_id
FROM
general_reservations a
INNER JOIN
routes b
WHERE
a.cancelled<>'2'
AND a.date_trip BETWEEN '$find_begin' AND '$find_end'
AND b.dep_symbol LIKE '$code'
AND b.id LIKE a.route_id
ORDER BY a.date_trip, b.dep_symbol, a.route_id";
Notice that I do have a.id and b.id from two different tables.
Use an alias name,
$sql = "SELECT a.id AS aid,
a.route_id,
a.requester,
a.reservation,
a.reservation_date,
a.reservation_by,
a.telephone,
a.email,
a.firstname,
a.lastname,
a.qty_pax,
a.date_trip,
a.time_trip,
a.trip_type,
a.cancelled,
a.notes,
a.room,
a.driver_id,
b.id AS bid,
b.dep_symbol,
b.dep_location_id
FROM
general_reservations a
INNER JOIN
routes b
WHERE
a.cancelled<>'2'
AND a.date_trip BETWEEN '$find_begin' AND '$find_end'
AND b.dep_symbol LIKE '$code'
AND b.id LIKE a.route_id
ORDER BY a.date_trip, b.dep_symbol, a.route_id";

Join but return ALL records from Table

I have the following SQL query:
SELECT * FROM `table1` INNER JOIN `table2` ON table1.messageid=table2.messageid WHERE `venue_active` = 1
The above works fine but it only returns fields where both tables have a messageid field.
My problem is that I need it to return ALL fields from Table1 reguardless if it has a messageid match in table2 or not.
So, in other words I need ALL records to be returned from Table1 and all records from Table2 where there's a messageid that matches both.
How can I do this?
Use a LEFT JOIN rather
SELECT *
FROM `table1` LEFT JOIN
`table2` ON table1.messageid=table2.messageid
WHERE `venue_active` = 1
That said, it will only work if venue_active is also part of table1, and not table2.
Have a look at the different scenarios
SQL Fiddle DEMO
Use a LEFT join rather than INNER
For example:
SELECT * FROM `table1`
LEFT JOIN `table2` ON table1.messageid=table2.messageid
WHERE `venue_active` = 1
Either you need a LEFT JOIN instead, or
a FULL OUTER JOIN workaround for MySQL:
SELECT
a.*,
b.*
FROM
table1 a
LEFT JOIN
table2 b ON a.messageid = b.messageid
WHERE a.venue_active = 1
UNION
SELECT
a.*,
b.*
FROM
table1 a
RIGHT JOIN
table2 b ON a.messageid = b.messageid;
WHERE a.venue_active = 1

How to get the first table where i know the id from second table

I need to get both table and here is the table structure
Table A
UserID
Username
Status
IntroCode
Table B
IntroCode
UserID
I want to get the table a data and join with table b on tblA.IntroCode = tblB.IntroCode, then get the username of tblB.userID. How can i do such join ?
I tried half way and stuck in the middle, please help. Thanks for reply
This is just a simple join.
SELECT a.*, b.* -- select your desired columns here
FROM tableA a
INNER JOIN tableB b
ON a.IntroCode = b.IntroCode
WHERE b.userid = valueHere
UPDATE 1
SELECT a.UserID,
a.`Username` OrigUserName,
a.`Status`,
c.`Username` IntroUserName
FROM tableA a
INNER JOIN tableB b
ON a.IntroCode = b.IntroCode
INNER JOIN tableA c
ON b.userID = c.userID
-- WHERE b.UserID = valueHere -- extra condition here
SELECT column_name(s)
FROM TableA
LEFT JOIN TableB
ON TableA.UserID=TableB.UserID
SELECT B.userID from TableA A
LEFT JOIN TableB B on A.IntroCode=B.IntroCode
select a.*,b.IntroCode from TableA a left join TableB b
on a.IntroCode = b.IntroCode
you have to give the columns with same name an unique value:
SELECT a.UserID as uid_a, b.UserID as uid_b
FROM tableA a
INNER JOIN tableB b ON a.IntroCode = b.IntroCode
WHERE b.UserID = 1
Use this query.
SELECT TableA.Username FROM TableA JOIN TableB ON (TableA.IntroCode = TableB.IntroCode);
use this query
SELECT * FROM tblA INNER JOIN tblB ON tblA.IntroCode = tblB.IntroCode where tblB.userid = value

Getting the maximum value from interrelated MySQL tables

I am trying to unify a pair of queries with a LEFT JOIN, so that I can perform an ORDER BY on a desired column.
Table A has an exclusive one-to-many relationship with Table B, and table B has an exclusive one-to-many relationship with Table C.
I want to get the maximum value of a column in Table C, for each row in Table A.
what I used to have was:
$tableA = SELECT * FROM tableA;
for each row in $tableA {
$maxValue = SELECT MAX(value) FROM tableC WHERE tableB_id IN (SELECT tableB_id FROM tableB WHERE tableA_id={$row['tableA_id']}) GROUP BY tableB_id;
}
and now I'm thinking along the lines of:
SELECT * FROM tableA LEFT JOIN (SELECT tableA_id, MAX(max_c_value) FROM (SELECT tableB_id, MAX(value) max_c_value FROM tableC GROUP BY tableB_id) t GROUP BY tableA_id) USING(tableA_id)
but I know that's gibberish. I am not sure where to go from here.
I'm finding it hard to explain the problem, sorry.
SELECT A.ID, MAX(C.MyField) as CField
FROM A
LEFT OUTER JOIN B
ON A.ID = B.A_ID
LEFT OUTER JOIN C
ON B.ID = C.B_ID
GROUP BY A.ID
You will get null value for CField if there is no rows corresponding.
select max(table3.field),table1.field from table1
join table2 on table1.id=table2.table1_id
join table3 on table2.id = table3.table2_id
group by table1.field
You were closer on the first try I think. You want something like this:
SELECT MAX(tableC.cValue) FROM tableA
LEFT JOIN tableB
ON tableA.tableA_id = tableB.tableA_id
LEFT JOIN tableC
ON tableB.tableB_id = tableC.tableB_id
http://www.w3schools.com/sql/sql_join.asp
I have managed to solve it on my own, only to come back here and find I was making things far too complicated for myself, getting lost in a sea of tables!
This code did actually work, but I have already replaced it with the simpler code suggested above:
SELECT * FROM A LEFT JOIN (
SELECT A_ID, MAX(val) FROM (
SELECT * FROM B LEFT JOIN (
SELECT B_ID, MAX(val) val FROM C GROUP BY B_ID
) x USING(B_ID)
) y GROUP BY A_ID
) z USING(A_ID);

Categories