Mysql joints is possible? - php

I have two table one table containing station id and station name, and another table containing id, name, startStationId, endStationId. I know second table's id by using that id i need to fetch all details of second table with station name for corresponding startStationId, endStationId.
ex: table1
---------------------------------
slNo staionId staionName
---------------------------------
1 0012 Bangalore ,
2 0014 Chennai ,
3 0015 Mumbai
---------------------------------
Table 2:
------------------------------------------
Id Name startStationId endStationId
-------------------------------------------
123 Dhinesh 0014 0015
-------------------------------------------
For example i know second table id(123).. so i want to fetch all results by using id, result would be.
Id =>123, Name => Dhinesh, StartStaion =>Chennai , Endstation=>Mumbai.
How can we write in one single query...?
Thanks in advance.

Try this.
SELECT t2.Id,t2.name,t1.StationName,t3.StationName
FROM table2 t2
INNER JOIN table1 t1 ON t2.startStationId = t1.stationId
INNER JOIN table1 t3 ON t2.endStationId = t3.stationId

SELECT t2.Id, t2.Name, tstart.stationName , tend.stationName
FROM table2 as t2
INNER JOIN table1 as tstart ON t2.startStationId = tstart.stationId
INNER JOIN table1 as tend ON t2.endStationId = tend.stationId
this should work

Related

Mysql php: How to join same table multiple times?

Team table has:
ID | TEAM
--------+----------
1 | A
2 | B
Result table has:
fk_ID1 | fk_ID2 | RESULT
----------+-----------+-----------
1 | 2 | 5:0
2 | 1 | 2:3
How to Inner JOIN table, to get: (A 5:0 B) & (A 2:3 B)?
My code example:
public function getResultList($limit, $offset) {
$query = " SELECT result_id,
t1.name name1,
t2.name name2,
team1_goals,
team2_goals,
date
FROM results
INNER JOIN team t1 ON fk_tm1_id=tm_id
INNER JOIN team t2 ON fk_tm2_id=tm_id";
$data = mysql::select($query);
return $data;
}
It's best to answer this as purely an SQL question, which it is. You need to assign a table alias when joining the same table two or more times.
You seem to only be assigning aliases to the column. To assign an alias to a column or table, you can add the alias directly after the column or table name (AS can also be used but isn't necessary for MySQL)
A common thing is to number the tables as t1, t2, t3, etc.
SELECT t1.name name1, t2.name name2 FROM ...
INNER JOIN team_table t1 ON ...
INNER JOIN team_table t2 ON ...
This aliases the first join as t1 and the second join as t2, which you would use when accessing data from that specific join (SELECT t1.name).

mysql fetch data from two tables

Please find picture to get more clarification on what I am asking
Click here
Let me tell you full description
I have two tables -
Table1 contains book_id and book_name
second table -
Table2 contains book_id, stock and cs_id
I want when someone choose cs_id so result displayed in terms of book_name and not book_id
I'm sorry for short description...
I have two tables in mysql database -
table1
SN ID Name
1 E-11 ELC
2 E-13 ELX
3 D-41 DME
and table2
SN ID CS_ID
1 E-11 C01
2 E-13 C01
3 D-41 C54
How to get result of all name from table1 using one cs_id from table2 using php.
Please help.
Thanks in advance!
Here what I'm using
$query = "SELECT table1.id, table2.name FROM table1 INNER JOIN table2 ON table1.id = table2.id WHERE table1.cs_id=$cs_id'
this is showing me 0 result.
Use TABLE LEFT JOIN
LIKE
select tb1.name,tb2.ID FROM TABLE1 as tb1 LEFT JOIN TABLE2 as tb2 ON tb1.ID=tb2.ID where tb2.CS_ID='$request_id';
I hope its work

join two tables and select records only if NOT in table 2

I would like to join two tables and only print records from table 1 where the rec_number is NOT in table 2.
table 1
name rec_number
john smith 123
Bob jonson 345
etc
Table 2
Bob jonson 345
etc
What is the query in php that would do this so the query only gives me John smith, not bob jonson.
is it:
$query = "select * from table1
left join rec_number on table1.rec_number = table2.rec_number";
$result=mysql_query($query);
Thank you.
You can use this query
select
t1.*
from table1 t1
left join table2 t2
on t2.rec_number = t1.rec_number
where t2.rec_number IS NULL
Beside the left join mentioned by Abhik, you could also use a subselect:
SELECT * FROM table1 WHERE table1.name NOT IN (SELECT name FROM table2);

mysql fetch all row based on join query and latest date time

Hello I have two tables:
table1 - eod_stock
company_code | open | high | low | ltp | close | ycp | total_volume | total_value | datetime
table 2 - company
ID | code | name
here company code = code so to get all name and other info i used this code:
but first one gives me error and 2nd one returns only one row, but i need all 200 companies with their associated info.
select
company.code,
company.name,
eod_stock.open,
eod_stock.high,
max(eod_stock.datetime)
from
company
right join company on company.code= eod_stock.company_code;
and
select
eod_stock.company_code,
max(eod_stock.close),
eod_stock.total_volume,
eod_stock.total_trade,
eod_stock.high,
eod_stock.low,
eod_stock.ltp,
max(eod_stock.datetime),
company.name
from
eod_stock
inner join company on (company.code = eod_stock.company_code);
but first one gives me error and 2nd one returns only one row, but i need all 200 companies with their associated info.
The trick here is to start with a list of the max datetime for each company_code, which you can do with this basic query:
SELECT company_code, MAX(datetime) AS maxdatetime
FROM eod_stock
GROUP BY company_code
Join this to a query that gets company code, company name, and end-of-day values, and you should be all set:
SELECT
company.code,
company.name,
eod_stock.open,
eod_stock.high
FROM eod_stock
INNER JOIN company ON company.code = eod_stock.company_code
INNER JOIN (
SELECT company_code, MAX(datetime) AS maxdatetime
FROM eod_stock
GROUP BY company_code) maxdt
ON maxdt.company_code = eod_stock.company_code AND
maxdt.maxdatetime = eod_stock.datetime
Your first error i guess that your have to write :
Table2 right join Table1
instead of company right join company
the 2nd one to get all company your full join !!
select
eod_stock.company_code,
max(eod_stock.close),
eod_stock.total_volume,
eod_stock.total_trade,
eod_stock.high,
eod_stock.low,
eod_stock.ltp,
max(eod_stock.datetime),
company.name
from
eod_stock
inner join company on (company.code = eod_stock.company_code) group by eod_stock.company_code;
dagfr was correct all i needed to add group by in the query.

MySQL insert data from other table

This question is similar to my previous question except this is INSERT instead of update
I have two tables: contacts and companies.
contacts has : id, group_id, company_id, email, company
companies has: id, group_id, name, email
so using this query
UPDATE contacts c
INNER JOIN companies co ON c.company_id = co.id
SET c.group_id = co.group_id,
c.company = companies.name
I can move update data from company to contacts where there contact.company_id = company.id.
But how can I do INSERT instead for all the company where does not have contact yet? or to make things simpler, how can I move all companies table data into contacts table data. e.g:
Companies
id group_id name email
1 1 abc a#a.com
2 1 def d#d.com
3 1 ghi g#g.com
Contacts
id group_id company_id email company phone
1 1 1 a#a.com abc
2 1 2 d#d.com def
3 1 3 g#g.com ghi
So I would like the entry like that, and for the one that is no value will be default to NULL or None
I think that you want:
INSERT INTO Contacts (id,group_id,company_id,email,name)
SELECT co.id,co.group_id,co.id,co.email,co.name
FROM company co
LEFT JOIN contacts c ON co.id = c.company_id
WHERE c.company_id IS NULL
This will insert all the information from contacts in company that wasn't already there. the column phone will be left null, since there is no information in contacts for that column.
I believe you would need to do an outer join between the 2 tables in your select statement then look for a field in your destination table to be null and only select those rows.
insert into t1 (f1,f2,f3) select s1,s2,s3 from sourcetable as st
left outer join t1 on
t1.f1=st.s1,
t1.f2=st.s2,
t1.f3=st.s3
where t1.id is null;
This way you only get the rows from st where there is no id in t1.

Categories