sql select using same table for join [closed] - mysql

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
Using mysql 4/5
I have 2 tables:
ROUTE
route_order (int)
zipcode
LOADS
load_id
pu_zipcode
do_zipcode
Goal:
select all rows from Loads where
pu_zipcode and do_zipcode are in Route AND
where the route_order pu_zipcode < route_order do_zipcode

I think this is what you are asking:
SELECT L.*
FROM Loads AS L
JOIN Route AS R1
ON R1.zipcode = L.PU_zipcode
JOIN Route AS R2
ON R2.zipcode = L.do_zipcode
WHERE R1.ROUTE_ORDER < R2.ROUTE_ORDER

Try this query
SELECT l.load_id, l.pu_zipcode, l.do_zipcode
FROM LOADS AS l, ROUTE AS r1, ROUTE AS r2
WHERE l.pu_zipcode=r1.zipcode
AND l.do_zipcode=r2.zipcode
AND r1.route_order<r2.route_order

Related

How can i get this SQL on yii2? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I am working with Yii2 and I need to use the Query Builder to convert the following raw SQL query where I am using a subquery in the inner join.
SELECT *
FROM class
INNER JOIN
(SELECT name, MAX(score) AS Maxscore
FROM class
GROUP BY name) topscore
ON class.name = topscore.name
AND class.score = topscore.maxscore;
You should show a bit of effort on your behalf as it is how it works here on SO, but as you are a new bee so i am adding an answer.
It is pretty straight forward see the Query builder guide, and you need to use the subquery in the following way
$subQuery = new \yii\db\Query();
$subQuery->select([new \yii\db\Expression('[[name]], MAX([[score]]) as Maxscore')])
->from('class')
->groupBy('[[name]]');
$query = new \yii\db\Query();
$query->select('*')
->from('class')
->innerJoin(['topscore'=>$subQuery])
->where(['=','class.[[name]]',new \yii\db\Expression('topscore.[[name]]')])
->andWhere(['=','class.[[score]]',new \yii\db\Expression('topscore.[[maxscore]]')])
->all();
Note: I didn't tested it thou but it should work correctly.

Inner join with 3 tables MySql [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
I have 3 tables (ativo, modelo, tipo) and I need get some columns of the tables (tipo and modelo).
Structure:
Ativo: id_ativo, name_ativo, fk_tipo_atv, fk_modelo_atv
Tipo: id_tipo, name_tipo
Modelo: id_modelo, name_modelo, fk_tipo_modelo
I need list "name - name_tipo - name_modelo"
Anyone can help me?
You haven't given us much to go on regarding what results you want, how the tables are linked and what you've tried but assuming that Ativo.fk_tipo_atv is a link to Tipo.id_tipo and Ativo.fk_modelo_atv is a link to Modelo.id_modelo:
SELECT a.name_ativo,t.name_tipo,m.name_modelo
FROM Ativo a
INNER JOIN Tipo t ON (a.fk_tipo_atv = t.id_tipo)
INNER JOIN Modelo m ON (a.fk_modelo_atv = m.id_modelo)

Listing specific data in Query [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm learning databases and I have a question: How can I list all the students that study in a specific school using joins?
My table is as follows:
X:schoolName(PK), SchoolAddress, SchoolTelephoneNumber
Y:schoolName(FK),StudentName,StudentNumber
How can i find out all the students name that study in 'London School' including there StudentNumber, SchoolAddress?
Try this:
SELECT
Students.StudentName,
Students.StudentNumber,
Schools.SchoolAddress
FROM
XSchools
INNER JOIN YStudents
ON Schools.schoolName = Students.schoolName
WHERE Schools.schoolName = 'London School'
SELECT
StudentName,
StudentNumber,
SchoolAddress
FROM
X
JOIN Y
WHERE Y.schoolName = 'London School'

SQL code for conversation messages like facebook [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
tbl_messages:
m_from----m_to----m_message----m_date--------m_time1000------1001----hello-------------2013-02-01----12:11:111001------1000-----hi----------------2013-02-01----13:10:111000------1001-----how r u?-------2013-02-01----16:19:111001------1000-----fine------------(2013-01-26)---12:11:111002------1003-----ur age?---------2013-03-10--13:14:111003------1002-----25----------------2013-03-11--13:36:151002------1000-----ur name?-------2013-02-04--13:52:441002------1000-----rihanna----------2013-05-15--13:11:541000------1002-----im there---------2013-02-01--13:34:111000------1003-----im here----------2013-02-01--13:04:00
For example user(1000)=Michael wants to see his messages.
Michael should only see users that send or receive message.
user(1000)=michael===messege send or receive====> user(1001)
user(1000)=michael===messege send or receive====> user(1002)
user(1000)=michael===messege send or receive====> user(1003)
I want SQL code that only users shows that Michael has a message exchange with them. The result be with last message (send or receive) ORDER BY (first) m_date (second) m_time
example: (this result is for user(1000))
user(1001)=david----------------------------------------------------time=16:19:11send:slice of message(how r...)-----------------------------------date=2013-02-01user(1002)=jenifer----------------------------------------------------time=13:11:54received:slice of message(rihanna...)----------------------------date=2013-05-15user(1002)=tom--------------------------------------------------------time=13:04:00send:slice of message(im here...)----------------------------------date=2013-02-01
Supposing a table tbl_users with fields u_id and u_name.
Something along the lines of
(SELECT m_from, u_name, u_time, 'sent', m_message, m_date
FROM tbl_messsages inner join tbl_users on tbl_messages.m_from = tbl_users.u_id)
UNION
(SELECT m_to, u_name, u_time, 'received', m_message, m_date
FROM tbl_messsages inner join tbl_users on tbl_messages.m_to = tbl_users.u_id)
ORDER BY m_date, m_time;
Now, MySQL can do fancy things for formatting, but if I read your examples correctly, it can't put them like that, most likely, and you don't want to. A database (MySQL and others) is not made to do complex format transformation, but to store and retrieve information. The formatting can be done outside in another language.

How to Convert This Sql in LinQ [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
Using this query I get my data which I want.
select *
from tbl_project
join tbl_project_detail on tbl_project_detail.project_id = tbl_project.id
I need to know how I can use this SQL in Linq-to-SQL:
select SUM(pd.no_vacancy) as Number
from tbl_project p
join tbl_project_detail pd on pd.project_id = p.id
where p.id='1'
I also try it but it doesn't work.
var result = (from p in db.tbl_projects
join c in db.tbl_project_details on p.id equals c.project_id
where c.project_id == type
select new { c.no_vacancy }).Sum();
Please help me.. I am stuck :(
I think this might be help you:
from p in tbl_project
join pd in tbl_project_detail on project_id equals p.id
where p.id=1
select new
{
sum(pd => pd.no_vacancy)
};
At Last I Solve My Problem By Using This
var result = (from p in db.tbl_projects
join
c in db.tbl_project_details on p.id equals c.project_id
where c.project_id == type
select (int)c.no_vacancy ).Sum();
Thank You Everyone For Helping Me.. :)