How to Laravel Mysql fetch data from database using multiple table join - mysql

This is my data retrieving query:
$admin_students=DB::select("select students.*,application_record.*,gender_record.*,
application_record.id as app_primary_id,application_record.status as app_status from students
left join application_record on application_record.student_id=students.id
left join application_record on application_record.gender_record_id=gender_record.name");
Table structure
Table 1 = application_record - > id, name, status, gender_record_id, ....
Table 2 = gender_record - > id, name
Now how can I return the Gender Name using the Gender Record ID which is the primary key for "Table 2"?

DB:: table('students')
->leftjoin('application_record, 'students.id','=' , 'application_record.student_id' )
->leftjoin('gender_record','application_record.gender_record_id','=','gender_record.id')->select('students.*',' application_record.*,
'gender_record.*,)->get();

Related

How can I select in table B depending on the activities took place in table A in mysql

I have a form but It has a dropdown which I populate dynamically From Table B , that part is fine.
Before data get populated in the drop down I have to check in the table where am going to post data (Table A) to see if there is a record for the user (userid) who is going to post and the data(data ID ) which is going to be posted, for the current date. If there is data for a different date I should proceed or if there is no data then still I have to proceed and populate the drop-down .
Table A I where I post fbgroupsid , userid , userid, dateposted , dropdowndata , dataID
Table B I select fbgroupsid , personid , dateposted , dropdowndata ,fbgroupsid.
I tried the below code but I did not get the right results. any help will be appreciated .
SELECT
TABLEb.fbgroupsid,
TABLEb.name,
TABLEb.fbgroupsid,
TABLEa.posteddate,
TABLEa.personid
FROM
TABLEb
INNER JOIN TABLEa
ON
TABLEb.fbgroupsid = TABLEa.fbgroupsid
WHERE TABLEa.posteddate != CURDATE();
You need to left join on table a on the id and the date being the current date and then get everything where the tableA fbgroupsid is null
set #personId = 1;
SELECT
TABLEb.fbgroupsid,
TABLEb.name
FROM
TABLEb
left JOIN TABLEa
ON
TABLEb.fbgroupsid = TABLEa.fbgroupsid
and DATE(TABLEa.posteddate) = CURDATE()
and TableA.PersonId = #personId
where tablea.fbgroupsid is null;
Here is an example in sqlFiddle. What is different either in the schema or the results from what you have or want?

SQL Query to retrieve username on reference from a child table

I am working with MySQL. I have two tables, fb_data and fb_not.
Columns of first table are :
id
username
useremail
userpass
fbappid
fbapplink
fbuserid
Columns of second table are :
fk_id
liked_one
liker_one
The liked_one and liker_one columns are referencing fbuserid of fb_data table. Simply they are foreign keys. What I want to do is, to get the names of liker_one column which is in fb_not table. And I want to get the names from username column of fb_data table.
This demonstration shows how the query should work. I want to get the user name of each liker from the fb_data table.
You can get your desired output by inner joining fb_not to fb_data twice; once for liker_one and once for liked_one as below .
SELECT DISTINCT CONCAT (
t2.username
,'('
,t2.fbuserid
,') is the liker who liked '
,t3.username
,'('
,t3.fbuserid
,') post'
) AS Col1
FROM fb_not t1
INNER JOIN fb_data t2 ON t1.liker_one = t2.fbuserid
INNER JOIN fb_data t3 ON t1.liked_one = t3.fbuserid
distinct is used to remove duplicate rows.
Result:
Col1
----------------------------------------------------
nadeem(1234) is the liker who liked nadee(4321) post
You can check demo here
Simply join the table twice:
SELECT d1.username as [Liker], d2.username as [Liked]
FROM fb_not f
INNER JOIN fb_data d1
ON d1.fbuserid = f.liker_one
INNER JOIN fb_data d2
ON d2.fbuserid = f.liked_one
Here is your requested answer. Thanks Let me know if I can help more.
SELECT
username
FROM
fb_data fdata
JOIN
fb_not datan
ON fdata.fbuserid = datan.liker_one

fetch data employee table with 1 id and multiple transaction id

here in transaction table one employee have multiple transaction .i want all transaction of 1 specific employee .but employee id should be print once in column .here is format
1.here one employee have more than one transaction id .id should display 1 time and display all transaction
desired result: employee details should display in one row but transaction id in multiple row
error id repeating id:
Copied from comments:
select
b.TransactionId,
a.Name
from
TransactionRecharge b
left JOIN Customer a on a.Id=b.CustomerId
where
b.CustomerId=101282
This is not a Good practice, I am pretty sure that "I am going to get negative marks for helping you", Anyway i just did for what you asked.
Below Query using ROW_NUMBER will help you get what you need exactly.
CREATE TABLE #customer(Id INT PRIMARY KEY,Name VARCHAR(255))
CREATE TABLE #TransactionRecharge(TransactionId VARCHAR(255),CustomerId INT FOREIGN KEY REFERENCES #customer(id))
INSERT INTO #customer
VALUES (1,'Raj'),(2,'Bala'),(3,'Chandra')
INSERT INTO #TransactionRecharge
VALUES ('reansaction1',1),('reansaction2',1),
('reansaction3',1),('reansaction4',1),
('reansaction5',1),('reansaction6',1),
('reansaction7',1),('reansaction8',2)
SELECT
CASE
WHEN ROW_NUMBER() OVER ( PARTITION BY a.id ORDER BY b.TransactionId,a.Id) = '1' THEN CONVERT(VARCHAR,a.Id)
ELSE ''
END AS Customer,
CASE
WHEN ROW_NUMBER() OVER ( PARTITION BY a.id ORDER BY b.TransactionId,a.Id) = '1' THEN Name
ELSE ''
END AS Name,
b.TransactionId
FROM
#TransactionRecharge b
LEFT JOIN
#customer a ON a.Id = b.CustomerId
WHERE 1 = 1
--AND b.CustomerId = 1
DROP TABLE #customer
DROP TABLE #TransactionRecharge

LIMIT Display of a different table field

Below are my tables:
1. tbluser
UserNumber - PK
Name
MemberType - Number
StationNumber - FK (connected to StationNo of tblStation)
2.tblStation
StationNo - PK
StationName
3.tblUserLogs
LogID - PK
UserID - FK (connected from UserNumber of tblusers)
LastLog
And all I want to do is to display Name (tblusers), StationName(tblStation) and LastLog(tblUserLogs) where MemberType is not equal to 1.
Here's my try...
SELECT tblusers.FirstName, tblstation.StationName, tblUserLogs.LastLog
FROM (tblstation INNER JOIN tblusers ON tblstation.StationNo = tblusers.StationNumber)
INNER JOIN tblUserLogs ON tblusers.UserNumber = tblUserLogs.UserID
WHERE (((tblusers.MemberType)<>1))
However, I get repeating records of my users. It displays ALL the LastLog data instead of showing the latest.
How should I do it?
Use this query:
SELECT tblusers.FirstName, tblstation.StationName, MAX(tblUserLogs.LastLog)
FROM (tblusers LEFT JOIN tblstation ON tblstation.StationNo = tblusers.StationNumber)
LEFT JOIN tblUserLogs ON tblusers.UserNumber = tblUserLogs.UserID
WHERE tblusers.MemberType != 1

how to Populating table with data returned from 3 joins tables

i have 4 tables
1. the first table(d_cities) for cities // related with the next table by country_id
CityId |CountryID |RegionID |City |Latitude|Longitude
2. the second table(d_country) for countries
CountryId|Country
3. the third table(ip2location_db11) for ip
ip_from|ip_to |country_code|country_name| city_name
4 the fourth table (ip_relation) would be like this
CountryID |CityId|ip_from |ip_to
i create the fourth table to collect custom data from the three tables and put it in one table..
this will has been done by :
join (d_country,d_cities) by id ,
then compare this names with IP table if matched
it will fetch the ids for these names & ips that matched and put it in the fourth table
..so i write my code like this and need to support to modify this code
INSERT ip_relations (CountryID, CityId,ip_from,ip_to)
SELECT *
FROM d_cities
INNER JOIN d_country ON d_cities.CountryID = d_country.CountryId
INNER JOIN ip2location_db11 ON ip2location_db11.country_name = d_country.Country
AND ip2location_db11.city_name = d_cities.City
/// this sql statement not work
first, i am not sure why do you design table like this.maybe you could change them like below:
d_cities: city_id | country_id | region_id |city | latitude| longitude
d_country: country_id | country
ip2location_db11: ip_from | ip_to | country_code | city_id
pS: I am not very sure what does country_code mean,so I keep it.base on the table structure above,it mostly like this: country to city is one-to-many and city_id must be unique, the ips is only have relation with the city_id.
I think ,this will be a better design...
then, if you have to solve the problem based on your current tables.
you would make a unique key "UNIQUE KEY uniq_from_to (ip_from,ip_to) "; and,there is sql:
INSERT IGNORE ip_relation
(SELECT cA.country_id,cA.city_id,ip.ip_from,ip.ip_to FROM ip2location_db11 ip
LEFT JOIN (SELECT cy.country,ct.city,ct.country_id,ct.city_id FROM d_country cy,d_cities ct WHERE cy.country_id = ct.country_id) as cA ON ip.city_name = cA.city AND ip.country_name = cA.country);
this means : 1.find All city-country groups;then based on the city-country groups;2.insert into your forth table,and when ip_from-ip_to is duplicate ,will cover the data before.
hope this can give you some help.
UPDATE ip_relations, ip2location_db11, d_cities, d_country
set ip_relations.countryid =d_country.CountryID
, ip_relations.cityid= d_cities.CityId
, ip_relations.ip_from=ip2location_db11.ip_from
, ip_relations.ip_to=ip2location_db11.ip_to
WHERE ip_relations.countryID=d_country.countryID and ip_relations.cityID=d_cities.CityID and ip_relations.ip_from=ip2location_db11.ip_from
and ip_relations.ip_to=ip2location_db11.ip_to;
Assuming country_id, city_id, countryname etc., being same through out all three tables:
SELECT
,dco.countryid AS countryid
,dci.cityid AS cityid
,ip_from
,ip_to
FROM d_country dco
INNER JOIN d_cities dci
ON dco.countryid=dci.countryid
INNER JOIN ip2location_db11 ip
ON TRIM(dci.city)=TRIM(ip.city_name)
AND TRIM(dco.country)=TRIM(ip.country_name)
WHERE dco.country_id=ip.country_code
UPDATE
ip_relation
FROM (
SELECT
,dco.countryid AS countryid
,dci.cityid AS cityid
,ip_from
,ip_to
FROM d_country dco
INNER JOIN d_cities dci
ON dco.countryid=dci.countryid
INNER JOIN ip2location_db11 ip
ON TRIM(dci.city)=TRIM(ip.city_name)
AND TRIM(dco.country)=TRIM(ip.country_name)
WHERE dco.country_id=ip.country_code
) DT
SET
ip_from=DT.ip_from
ip_to=DT.ip_to
WHERE CountryID=DT.countryid
AND CityId=DT.cityid
The correct way to join is here
UPDATE
ip_relations ir
INNER JOIN ip2location_db11 idl ON ir.ip_to = idl.ip_to
INNER JOIN ip2location_db11 idr ON ir.ip_from = idr.ip_from
INNER JOIN d_cities dc ON ir.d_cities = dc.d_cities
INNER JOIN d_country dct ON ir.countryID = dct.countryID
SET
ir.CountryID = dct.CountryID,
ir.CityId = dc.CityId,
ir.ip_from = dcr.ip_from,
ir.ip_to = dcl.ip_to
// put where condition if required
But when you are joining on some keys and want to update the keys i am sure all the keys will be same even after update so this will have no effect. If you update some else columns then it is practical.
To understand this assume this example.
joining two table on key which is 3. 3 is coming from 2nd table. updating column of first table with 3. So why do you need this? You need to update some else columns not the same ones you are joining.
INSERT INTO ip_relations (CityId,CountryID,ip_from,ip_to)
SELECT
d_cities.CityId,
d_cities.CountryID,
ip2location_db11.ip_from,
ip2location_db11.ip_to
FROM d_cities
INNER JOIN d_country ON d_cities.CountryID = d_country.CountryId
INNER JOIN ip2location_db11 ON ip2location_db11.country_name = d_country.Country
AND ip2location_db11.city_name = d_cities.City