开发者

How to select fields from one table and order them by fields from another table

I have two tables.

Structure of first table:

id
secondtableid
value

Structure of second table:

id
title

How can I select fields 'id' from开发者_开发百科 first table know value of 'value' column and order result by value of 'title' column of second table, if secondtable id is id of second table?


You can order by fields you don't select.

select FirstTable.id, FirstTable.value
    from FirstTable
        inner join SecondTable on (FirstTable.SecondTableID=SecondTable.ID)
    order by SecondTable.Title


:)

select id from first_table 
inner join second_table on first_table.secondtableid = second_table.id
where value = 'Known Value'
order by second_table.title


Something like this:

select 
    firsttable.id, firsttable.value, secondtable.title 
from
    firsttable 
join
    secondtable on firsttable.secondtableid = secondtable.id 
order by 
    secondtable.title


This a beginner question, so I will answer as simple as possible:

SELECT t1.id, t1.value
FROM       table1 t1 
INNER JOIN table2 t2 
ON t1.secondtableid = t2.id 
ORDER BY t2.title

You can JOIN the tables, indicating how they join.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜