How to avoid duplicate field name in query result - ruby

I am developing a padrino web application. I have two tables:
User table: Has id, name, address fields
Post table: Has id, user_id, content
If I join them,
User.join(:posts, user_id:, :id)
they return two fields with the same name id:
id name address id user_id content
1 jim *** 3 1 post1
I would like to rename each of these ids. I want the result to be:
u_id name address p_id user_id content
1 jim *** 3 1 post1
I am using Sequel adapter for postgresql db in padrino project.
In mysql query, it will be like this:
select u.id as u_id,
u.name,
u.address,
p.id as p_id,
p.user_id as user_id,
p.content
from users u
join posts p on u.id = p.user_id
what should I do?
What I want is not sql query, but code of ruby language. Thanks.

You should use Dataset#select to set which columns are being selected:
User.from{users.as(:u)}.join(Sequel[:posts].as(:p), user_id: :id).
select{[u[:id].as(:u_id), u[:name], u[:address],
p[:id].as(:p_id), p[:user_id].as(:user_id), p[:content]]}

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');

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

Insert and Update in Single Table using Merge?

I want to Update or insert Oracle Table based on condition.
Consider that in the table have 2 columns (like id and name), one or more name having the same id. In this situation i want to check the id and name (like 1,'Buyer'), if its exist then i want to update name 'Buyer' to 'Service Provider'. otherwise i want to just insert the values (1,'Service Provider').
I tried this through Merge, but it's update all the name column of id 1 to 'Service Provider'.
merge into party_type p
using (select 1 party_id, 'Buyer' party_type from dual) t
on (t.party_id = p.party_id)
when matched then
update set party_type = 'Service Provider'
when not matched then
insert (party_id,party_type) values(1,'Service Provider');
Available data in the table:
1 Buyer
1 Buyer Agent
1 Vendor
Thanks in advance.
you need to join on both columns
merge into party_type p
using (select 1 party_id, 'Buyer' party_type from dual) t
on (t.party_id = p.party_id and t.party_type = p.party_type)
when matched then
update set party_type = 'Service Provider'
when not matched then
insert (party_id,party_type) values(1,'Service Provider');

sorting with related tables in mysql

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

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