sorting with related tables in mysql - sql-order-by

I cant remember the right aproach in php to sort data from related tables anymore? Iam not sure if its even possible in one query?
table brands
id name
------------
1 Disney
2 Pepsi
3 Sony
table products
id name brandId
-----------------------
1 cd-playerX 3
2 nice poster 1
3 usb-radio 3
4 cd-playerY 3
I want to list all the products sorted by the name of the brand table (order=asc) like this:
nice poster (Disney)
cd-playerX (Sony)
cd-playerY (Sony)
usb-radio (Sony)
Anyone?

select p.name + ' (' + b.name + ')' as fullName
from products as p
left join brands as b
on p.id = b.brandId
order by b.name asc, p.name asc --optional for brands with multiple products

Related

can i use join in stored procedures - oracle database

I have two tables product and condition where product_id is primary key of product and its foreign key in condition.
product
product_id name
1 eggs
2 milk
condition
product_id condition_name
1 new
2 bad
1 normal
I need a procedure which can give me the name of the all product which is not have bad condition.
You can simply use single query as follows:
select p.name
from product p
where not exists (select 1 from condition c where p.product_id = c.product_id
and c.condition_name = 'bad');

Display results as groups by date, date as title for every group

Damn, there must be answers for this here but i have no idea where to search for.
I'm getting some data out of my database and group this by a date in one of the tables. This all works fine, but now i want a title with this date above every group. So something like this:
27 januari
- news 1
- news 2
- news 3
26 januari
- news 4
- news 5
etc. ect.
This is my query:
$sql = "SELECT
vd.*, tbl.name, gs.game_title
FROM
vods vd
INNER JOIN
tabel tbl on vd.tabel_id = tbl.id
LEFT JOIN
games gs on vd.game_id = gs.id
WHERE
vd.active = 1
GROUP BY
vd.tabel_date
ORDER BY
vd.tabel_date DESC";
Do i really have to run a query for each date? Or is there some other way of doing this?

count of product with parent_category_id

I have two tables
table 1: cat_a
table 2:prod_a
Out put must be
When category_id =1 then product_id count must be 7
when category_id = 3 then product_id count must be 5
when category_id = 6 then product_id count must be 3
when category_id = 7 then product_id count must be 2
when category_id = 5 then product_id count must be 2
Please help me . I have searched lot of questions and answers and forums. couldn't find exact solutions.
I don't want spoon feeding answers . Please mention the hint or way with answer.
My problem resloved.
SELECT count( product_id)
FROM prod_A
WHERE category_id IN (SELECT category_id FROM cat_A START WITH category_id = <i_category_id>
CONNECT BY PRIOR category_id = parent_catogory);

Merging two tables to one parent table

I have a table with 2 columns ID, Name, Now I am trying to make a two different data with the same table that will come around 2 new tables but I want both the table rows merged into parent table.
I will explain with sample data:
Parent table:
ID Name
1 abc
2 def
I am writing a select query as
Select ID, Name||'_First' as a from table
This will give me
ID Name
1 abc_First
2 def_First
Now my another select query as
I am writing a select query as
Select ID, Name||'_Second' as b from table
This will give me
ID Name
1 abc_second
2 def_Second
Now I am trying to join both the queries and produce the parent table as
Tried like this:
Select ID,a,b from
(Select ID, Name||'_First' as a from table
Inner join
Select ID, Name||'_Second' as b from table)
on joins here
But this is producing me 3 columnslike
ID a b
1 abc_First abc_second
2 def_First def_Second
But I need as
ID Name
1 abc_First
2 def_First
1 abc_second
2 def_Second
I am stuck at this point.
Use union all
Select ID, Name||'_First' as name from table
union all
Select ID, Name||'_Second' as name from table

LINQ Query for fetching data from Multiple Tables

I am working in Asp.Net 4.0 C#-- MVC-3. I have one problem and don't know how to solve. I have 4 tables and want to fetch data from that tables with LINQ.
TABLES
1) Project_Master
Field Names :
project_id (pk)
project_name
company_id (FK with company_master)
company_category_id (FK with Company_Category_master)
project_status_id (FK with Project_Status_Master)
2)Company_Master
Field Names :
company_id
company_name
company_category_id (FK with Company_Category_Master)
3) Company_Category_Master
Field Names :
Company_Category_Id
Company_Category_Name
4) Project_Status_Master
Field Name :
Project_Status_Id
Project_Status_Name
Below are the fields I need to fetch..(using LINQ Query)
Company_Name
Total completed project using status id(1)=complete (where staus 1 means completed)
Total Project
Company_category_name
So, how can I fetch data with linq query??
Thanks in advance...
Try the below example:
(From lse In Me.Leases, nty In Me.Entities, psg In Me.ProductionStages, lsg In LeaseStages _
Where lse.LeaseName = leaseName _
Select lse, lsg, nty, psg).Single
or you can use below example too:
var employeesQuery = from populationTable in db.Populations
join personTable in db.Persons on populationTable.Guid equals personTable.PopulationGuid
join employeeTable in db.Employees on personTable.Guid equals employeeTable.PersonGuid
select new { populationTable, personTable, employeeTable};

Resources