I want to use SUM function in alias
please look at following code i hope you understand it:
SELECT PATIENTS.NAME,
PATIENTS.FAMILY,
COST.COST,
COST.REALDATEYEAR,
COST.REALDATEMONTH,
COST.REALDATEDAY,
CONTRACTS.ID,
CONTRACTS.FRANSHIZ AS CO1,
PATIENTS.COMPANY,
CONTRACTS.COMPANY,
COST.TYPE,
TARRIFS.ID,
TARRIFS.ACTION,
TARRIFS.COST AS CO
FROM PATIENTS
INNER JOIN COST
INNER JOIN CONTRACTS
INNER JOIN TARRIFS
ON PATIENTS.SINGLE_ID = COST.SINGLE_ID
AND COST.TYPE = TARRIFS.ID
AND CONTRACTS.ID = PATIENTS.COMPANY
i want to use SUM for co1 and co,
You need to use GROUP BY when using SUM.
Your query something like this.
SELECT PATIENTS.NAME,
PATIENTS.FAMILY,
COST.COST,
COST.REALDATEYEAR,
COST.REALDATEMONTH,
COST.REALDATEDAY,
CONTRACTS.ID,
SUM(CONTRACTS.FRANSHIZ) AS CO1,
PATIENTS.COMPANY,
CONTRACTS.COMPANY,
COST.TYPE,
TARRIFS.ID,
TARRIFS.ACTION,
TARRIFS.COST AS CO
FROM PATIENTS
INNER JOIN COST
INNER JOIN CONTRACTS
INNER JOIN TARRIFS
ON PATIENTS.SINGLE_ID = COST.SINGLE_ID
AND COST.TYPE = TARRIFS.ID
AND CONTRACTS.ID = PATIENTS.COMPANY
GROUP BY CONTRACTS.FRANSHIZ
Related
I have created an INNER JOIN query as shown below and was wondering how I can make it work? I need for the HomeTeam and AwayTeam to equal TeamID in the query. Any help would be much appreciated. Thanks
$result = mysqli_query($con,"SELECT results.*,team.TeamName
FROM results
INNER JOIN team ON team.TeamID = results.HomeTeam
INNER JOIN team on team.TeamID = results.AwayTeam");
You need to use aliases for the table that you are including twice. Otherwise mysql cannot distinguish between the two.
To be able to process the results easily, you can do the same with the names you are selecting.
Something like:
SELECT
results.*,
t1.TeamName AS TeamNameHome,
t2.TeamName AS TeamNameAway
FROM results
INNER JOIN team t1
ON t1.TeamID = results.HomeTeam
INNER JOIN team t2
ON t2.TeamID = results.AwayTeam
I have normalized tables I want to select the items that belong to the userid
I'm familiar with select syntax but I'm very weak in joins tables so I'm a bit confused on how to get the items that belong to the user should I use join ? or is there other way
this is just simple example of my tables they have more fields
..........
user
..........
userid
firstname
address
..........
items
..........
itemsid
itemName
itemDescription
..........
user_items
..........
userid(FK)
itemsid(FK)
Use two inner join
select a.*, b.*
from user_items as c
inner join user as a on a.userid = c.userid
inner join items as b on b.itemsid = c.itemsid;
Use INNER JOIN
SQL
select user.*, items.*
from user_items
inner join user on user.userid = items.userid
inner join items on items.itemsid = user_items.itemsid;
So if I read this correctly, user_items.userid = user.userid.
So you want to join, something like this.
SELECT i.itemsid, i.itemName, i.Description FROM items i JOIN users us ON ui.userid = us.userid JOIN user_items ui ON ui.itemsid = i.itemsid WHERE ui.userid = VALUE;
Replace Value with your actuall user id
I am using php and I have to get the data from multiple tables with common id, but the problem is that in few tables that common id contains multiple records,using inner join gives me separate rows of data e.g.
{"dish_id":"52","quantity":"1","STATUS":"pending","franchise_id":"5","order_type":"PickUp","extraId":"2"}
{"dish_id":"52","quantity":"1","STATUS":"pending","franchise_id":"5","order_type":"PickUp","extraId":"3"}
extraId is the multiple record for the dish_id:52.
I need result like this.
{"dish_id":"52","quantity":"1","STATUS":"pending","franchise_id":"5","order_type":"PickUp","extraId"[{"id":"2"},{"id":"3"]}}
My query is:
$orders = "Select DISTINCT order_detail.dish_id,order_detail.quantity,order_detail.STATUS,
order_main.franchise_id,order_main.order_type,
order_extras.extra_id,order_extras.extra_title,
order_addons.addon_id,order_addons.addon_size
from order_main
INNER JOIN order_detail ON order_main.id=order_detail.order_id
INNER JOIN order_extras ON order_main.id=order_extras.order_id
INNER JOIN order_addons ON order_main.id=order_addons.order_id
WHERE order_main.franchise_id='$storeId'
and
order_detail.STATUS!='$order_status'";
please help.
Use group by and group_concat. Something like this:
Select d.dish_id, d.quantity, d.STATUS, m.franchise_id, m.order_type,
group_concat(e.extra_id) as extraids
from order_main m INNER JOIN
order_detail d
ON m.id = d.order_id INNER JOIN
order_extras e
ON m.id = e.order_id INNER JOIN
order_addons a
ON m.id = a.order_id
where m.franchise_id = '$storeId' and d.STATUS <> '$order_status'
group by d.dish_id, d.quantity, d.STATUS, m.franchise_id, m.order_type;
Your desired results do not include these columns:
e.extra_title
a.addon_id
a.addon_size
I would also suggest that you remove the join to order_addons.
Notice that table aliases make the query easier to write and to read.
You can use group bywith dish_id
$orders = "Select DISTINCT order_detail.dish_id,order_detail.quantity,order_detail.STATUS,
order_main.franchise_id,order_main.order_type,
order_extras.extra_id,order_extras.extra_title,
order_addons.addon_id,order_addons.addon_size
from order_main
INNER JOIN order_detail ON order_main.id=order_detail.order_id
INNER JOIN order_extras ON order_main.id=order_extras.order_id
INNER JOIN order_addons ON order_main.id=order_addons.order_id
WHERE order_main.franchise_id='$storeId'
and
order_detail.STATUS!='$order_status' group by order_detail.dish_id";
This is the table structure
plinks
link
ojectidfk
uidfk
ojects
ojectid
uidfk
ojectname
Try
useridfk
ojectidfk
goryidfk
gory
goryid
goryname
What i want to do is select the ojectname from ojects where the plinks projectidfk is the same in ojects and plinks but select everything from try where the ojectid is equal to the ojectsid where pinks link = 8493284 AND gory id = try goryidfk
Select obj.objectnamem, try.* from objects as obj
inner join plinks on plinks.projectidfk = obj.ojectid
inner join try on try.projectidfk = obj.ojectid
inner join gory on gory.goryid = try.goryidfk
where plinks.link = 8493284
Try joining all four tables and use where clause for link like:
SELECT o.objectName, t.*
FROM ojects o INNER JOIN plinks p ON o.ojectId = p.ojectidfk
INNER JOIN try t ON o.ojectid = t.ojectidfk
INNER JOIN gory g ON g.goryid = t.goryidfk
WHERE p.link = 8493284
Using simple JOIN should do this for you. You just connect tables by constraints and then retrieve whatever you want from any of those tables.
Please notice, that if you write a pseudo-code it would look pretty similar to the actual code. You need to use INNER JOINs because you want to be sure, that there are rows that share all those relations and match your criteria on plinks.link.
SELECT o.ojectname, t.*
FROM
ojects o
INNER JOIN plinks p ON p.ojectidfk = o.ojectid
INNER JOIN try t ON t.ojectidfk = o.ojectid
INNER JOIN gory g ON g.goryid = t.goryidfk
WHERE
p.link = 8493284
I'm a little confused in here and need some help...
the situation is I've made three tables(fr_Leagues, fr_nations and fr_confeds), all i want to do is add a league which shows the name of the categories not the i.d with pagination. Here is the code:
NOW FIXED!
"SELECT
a.id as confed_id,
a.fr_short_name as confed_name,
b.id as nation_id,
b.fr_name as nation_name,
c.id as league_id,
c.fr_name as league_name"
." FROM fr_confeds as a
INNER JOIN fr_nations as b ON a.id = b.confed_id
INNER JOIN fr_leagues as c ON b.id = c.nation_id"
." LIMIT $paginate->start, $paginate->limit"
You are missing on how to link the different tables together. On each INNER JOIN, you need to have it:
INNER JOIN fr_nations ON a.<someColumn> = b.<anotherColumn> INNER JOIN fr_leagues ON a.<someColumn> = b.<anotherColumn>
USE THIS QUERY
SELECT * FROM fr_confeds as A
INNER JOIN fr_nations as B ON A.id = B.confed_id
INNER JOIN fr_leagues as C ON B.confed_id = C.league_id
LIMIT $paginate->start, $paginate->limit