I want to join two table with query builder in laravel - laravel

I have Two table T1 and T2.
T2 is Making From T1.
T1 table:
ID Country Name
1 Bangaldesh
2 India
3 USA
4 UK
T2 table making from T1 ID
ID From T1 table C1 From T1 table C2
1 1(Bangladesh) 2(India)
2 2(India) 3(USA)
3 2(India) 1(Bangladesh)
4 2(India) 1(Bangladesh)
How can i Joining now Two Table for laravel query builder?
I need to show country name by ID
Thanks for Advance.

Check the documentation on joins.
DB::table('T2')->join('T1', 'T1.id', '=', 'T2.id_column')->get();
Assuming that the column in T2 that has the T1 id is called id_column.

Related

Inserting selected data from table in one schema into another table in a different schema

I have a table A in Schema1 and table B in Schema2.
The tables have different columns.
Table A:
ID1 Name Code
-------------------------------
1 Skyler A0
2 Amanda A1
3 Rachel B0
4 Harvey C0
5 Louis B1
Table B:
ID Names Enterprise Modified_Date
------------------------------------------------------
1 Amanda 1 2018.08.10
2 Skyler 1 2018.08.11
As depicted, Schema1.A.Name = Schema2.B.Names
I want to insert the values "Rachel,Harvey and Louis" from A.Name into B.Names.
For b.ID, i have a sequence in place. Enterprise column is always 1 and modified date can e sysdate.
How can i achieve this in PL/SQL?
use insert Statement with select statement
insert into tabB (names,Enterprise,Modified_Date )
select Name,1,sysdate from tabA where Name in ('Rachel','Harvey','Louis');
You can do this by using below query.
insert into tableB (names,Enterprise,Modified_Date )
select Name,1,sysdate from tableA where Name not in (select distinct(Name) from tableB);

compare two tables and delete rows from one table if there are similar coumn values in two tables hive

Table description are in the link
Table 1 and Table 2 has rows with A and D .I need these two removed from Table 2 .
Please check the link below for descriptive detail . Thank you .
You may do an INSERT OVERWRITE using a LEFT JOIN select query.
INSERT overwrite TABLE table2
SELECT t2.*
from table2 t2
LEFT JOIN table1 t1
on (t1.x = t2.p) --use appropriate common column name
WHERE t1.x is NULL; --where there's no common element in t2

Need help creating a view on top of a JOIN query that needs to return only the latest value

I need help with my SQL Query I have Two tables that i need to join using a LEFT OUTER JOIN, then i need to create a database view over that particular view. If i run a query on the join to look for name A i need to get that A's latest brand "AP".
Table 1
ID name address
-----------------------
1 A ATL
2 B ATL
TABLE 2
ID PER_ID brand DATEE
--------------------------------------------
1 1 MS 5/19/17:1:00pm
2 1 XB 5/19/17:1:05pm
3 1 AP 5/19/17:2:00pm
4 2 RO 5/19/17:3:00pm
5 2 WE 5/19/17:4:00pm
I tried query a which returns correct result but i get problem 1 when i try to build the database view on top of the join. I tried query b but when i query my view in oracle sql developer i still get all the results but not the latest.
query a:
SELECT * from table_1
left outer join table_2 on table_1.ID = Table_2.PER_ID
AND table_2.DATE = (SELECT MAX(DATE) from table_2 z where z.PER_ID = table_2.PER_ID)
Problem 1
Error report -
ORA-01799: a column may not be outer-joined to a subquery
01799. 00000 - "a column may not be outer-joined to a subquery"
*Cause: <expression>(+) <relop> (<subquery>) is not allowed.
*Action: Either remove the (+) or make a view out of the subquery.
In V6 and before, the (+) was just ignored in this case.
Query 2:
SELECT * from table_1
left outer join(SELECT PER_ID,brand, max(DATEE) from table_2 group by brand,PER_ID) t2 on table_1.ID = t2.PER_ID
Use row_number():
select t1.id, t1.name, t1.address, t2.id as t2_id, t2.brand, t2.datee
from table_1 t1 left outer join
(select t2.*,
row_number() over (partition by per_id order by date desc) as seqnum
from table_2 t2
) t2
on t1.ID = t2.PER_ID and t2.seqnum = 1;
When defining a view, you should be in the habit of listing the columns explicitly.

Oracle join query

I have two tables
Table A has columns id|name|age.
Table B has columns id|name|age.
Sample Records from table A
1|xavi |23
2|christine|24
3|faisal |25
5|jude |27
Sample Records from table B
1|xavi |23
2|christine|22
3|faisal |23
4|ram |25
If id values from table A matches in table B than take records from table A only.
Also take records which are present in table A only
Also take records which are present in table B only
So my result should be
1|xavi |23
2|christine|24
3|faisal |25
4|ram |25
5|jude |27
You can simply use union operator to get unique values from both tables. Operator UNION will remove repeated values.
SELECT * FROM tableA AS t1
UNION
SELECT * FROM tableB AS t2
You have a precedence problem here. Take all the records from table A and then the extra records from table B:
select *
from A
union all
select *
from B
where B.id not in (select A.id from A);
You can also express this with a full outer join (assuming id is not duplicated in either table):
select coalesce(A.id, B.id) as id,
coalesce(A.name, B.name) as name,
coalesce(A.age, B.age) as age
from A full outer join
B
on A.id = B.id;
In this case, the coalesce() gives priority to the values in A.
select distinct * FROM
(
select ID, NAME, AGE from TableA
UNION ALL
select ID, NAME, AGE from TableB
) TableAB
Some things to consider --> Unless you're updating specific tables and the records are the same, it will not matter which table you're viewing the records from (because they're the same...).
If you want to see which table the records are deriving from, let me know and i'll show you how to do that as well... but the query is more complex and i don't really think it's required for the purpose described above. let me know if this helps... thanks, Brian
If the tables has relation you need:
Select DISTINCT *
from tableA a
Inner Join tableB b
On a.id = b.id
If not:
You have to use UNION and after using DISTINCT.
DISTINCT will not permit repeat rows.

Sql Query Help in select statement/Oracle Discoverer

I have a table as follows.
Id Code Indicator
1 AB
1 CD Y
1 EF
2 BC Y
3 AB
4 GH
4 AB Y
5 CD
5 BC
Now I need to retrieve the ID's which do not have any indicator associated to them. In this case, the retrieved rows should be
ID Code Indicator
3 AB
5 CD
5 BC
Thanks to y'll I got it in sql but I have the same table as a view in Oracle discoverer. How do i write a report to get the same result?All help much appreciated!!
This should do it (Warning: Untested):
select id, code
from table
where id not in (select id from table where indicator='Y')
SELECT *
FROM TABLE t1
WHERE T1.ID in (SELECT
t2.ID
FROM Table t2
GROUP BY t2.ID
HAVING MAX(t2.Indicator) = 'Y')

Resources