Selecting columns from table after joining in SQL [duplicate] - mysql

This question already has answers here:
Should I Always Fully Qualify Column Names In SQL?
(11 answers)
JOIN - Is it a good practice to add table name before column name?
(3 answers)
Closed 6 months ago.
is there a difference between specifying the table-column after joining tables?
For example, in the below queries I selected the columns as b.refunded_at or refunded_at. The output the same, but I just want to make sure.
Thanks
SELECT billing_cycle_in_months,
MIN(DATEDIFF(refunded_at, settled_at)) AS min_days,
AVG(DATEDIFF(refunded_at, settled_at)) AS avg_days,
MAX(DATEDIFF(refunded_at, settled_at)) AS max_days
FROM noom_signups AS a
JOIN noom_transactions AS b ON a.signup_id = b.signup_id
JOIN noom_plans AS c ON a.plan_id = c.plan_id
WHERE started_at >= 2019-01-01
GROUP BY 1;
and
SELECT billing_cycle_in_months,
MIN(DATEDIFF(b.refunded_at, b.settled_at)) AS min_days,
AVG(DATEDIFF(b.refunded_at, b.settled_at)) AS avg_days,
MAX(DATEDIFF(b.refunded_at, b.settled_at)) AS max_days
FROM noom_signups AS a
JOIN noom_transactions AS b ON a.signup_id = b.signup_id
JOIN noom_plans AS c ON a.plan_id = c.plan_id
WHERE a.started_at >= 2019-01-01
GROUP BY 1;

Tables do not have to be specified so long as the column names are unambiguous.

Related

How to merge the result of two select mysql query [duplicate]

This question already has answers here:
How can I get multiple counts with one SQL query?
(12 answers)
Closed 1 year ago.
my first query is
select GEAR,count(GEAR)
from new_failure
where STN_CODE = "BVH" group by(Gear);
and its result is
result if image is not visible
# GEAR Total
SIGNAL 8
POINT 16
HASSDAC 5
,SIGNAL 1
SSDAC 1
TRACK CIRCUIT 9
UFSBI 2
DC 1
2nd query
select GEAR,count(GEAR)
from new_failure
where STN_CODE = "BVH"
and MONTH(fail_time) = 4
group by(Gear);
result
# GEAR April
SIGNAL 3
POINT 4
HASSDAC 1
,SIGNAL 1
SSDAC 1
i want result in the form given in image below
You can use either LEFT JOIN, RIGHT JOIN or JOIN depending on what you are aiming to get,
SELECT *
FROM ( select GEAR,count(GEAR)
from new_failure
where STN_CODE = "BVH" group by(Gear) AS A
JOIN ( select GEAR,count(GEAR)
from new_failure
where STN_CODE = "BVH"
and MONTH(fail_time) = 4 AS B
ON A.orders_id=B.orders_id
or you can refer to this link for a similar question
joining two select statements

How to show PHP MySQLi having count result in webpage? [duplicate]

This question already has answers here:
select count(*) from table of mysql in php
(12 answers)
Closed 3 years ago.
I can fetch the desired record using MySQLi query directly in the database. But how to reflect the count in the webpage as it shows data in the result-set of phpMyAdmin interface?
Below is the MySQLi query I have used to fetch the below detail.
SELECT `recordUniqueID`, COUNT(*) FROM `status_data` WHERE
`status_data`.`statusName` = '1'
AND `status_data`.`emailAddress` = 'shashibhushan.roy#abcd.com'
AND `status_data`.`recordCreatedDateTime` BETWEEN '2018/11/25 10:00' AND '2019/03/02 10:00'
GROUP BY `recordUniqueID`
HAVING COUNT(*) >= 1
ORDER by id ASC
Below is the result I got in phpMyAdmin interface
Below is the screen from my webpage where I want to show the count of respective rows.
The yellow space I want to fill with the count of the respective rows (repeat values).
https://i.stack.imgur.com/9UeDn.png
https://i.stack.imgur.com/Dmy2w.png
You could add an alias to you count column (eg: my_count)
SELECT `recordUniqueID`, COUNT(*) my_count
FROM `status_data`
WHERE `status_data`.`statusName` = '1'
AND `status_data`.`emailAddress` = 'shashibhushan.roy#abcd.com'
AND `status_data`.`recordCreatedDateTime` BETWEEN '2018/11/25 10:00' AND '2019/03/02 10:00'
GROUP BY `recordUniqueID`
HAVING COUNT(*) >= 1 ORDER by id ASC
and then you could echo the value accessing using the alias
assumin the resulting rows are returned in a $row var :
echo $row['my_count'];

SQL check in two tables [duplicate]

This question already has answers here:
SELECT * WHERE NOT EXISTS
(5 answers)
Closed 8 years ago.
Hope you can help me.
I have 2 tables in my MqSQL database.
Table 1.
speedywebs_data
cardid
1
2
3
4
Table 2:
Speedywebs_results
resultid / card1 / card2
1 / 2 / 1
2 / 4 /4
my problem is, i want to get all posts in speedywebs_data table, but only cardid's whos not listed in the speedywebs_results cardid1. How can i do that?
SELECT speedywebs_data.*
FROM
speedywebs_data
WHERE
cardid NOT IN (SELECT card1 FROM Speedywebs_results WHERE card1 IS NOT NULL)
or you could use also this:
SELECT speedywebs_data.*
FROM
speedywebs_data LEFT JOIN Speedywebs_results
ON speedywebs_data.cardid = Speedywebs_results.card1
WHERE
Speedywebs_results.card1 IS NULL
You wanna check if a value doesn't exist ?
So use... NOT EXISTS.
select cardid
from speedywebs_data swd
where not exists (select null
from speedywebs_results swr
where swr.card1 = swd.cardid)

Multiple aggregate function for mysql. like SUM [duplicate]

This question already has answers here:
Get rows product (multiplication)
(3 answers)
Closed 9 years ago.
How can I multiple numbers from table to one result like SUM() does?
In "table1" I have column "number1" with these values:
table1.number1
--------------
1
2
3
I try this sql:
SELECT #multiple := #multiple number1 as mul
FROM table1
and I got this:
mul
---
1
2
6
But I need just the last row with the value: 6
without using ORDER DESC
Try this:
SELECT MAX(#multiple := #multiple * number1) AS mul
FROM table1, (SELECT #multiple:=1) a;

Return n number of rows from SQL Server query [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Row Offset in SQL Server
I have a query which has a like condition and it returns around 1500 rows. Now I want to know how can I get the rows part by part, e.g. return 75 rows, then again the next 75 rows until it reaches the end of the rows. I know I can use something like SELECT TOP 75
I tried something like this but it does not return any rows
SELECT *
FROM
(SELECT ROW_NUMBER() OVER (ORDER BY WarehouseSubType.id) as row,
WarehouseSubType.id,
WarehouseType.name as WarehouseTypename,
WarehouseType.alternateName AS WarehouseTypealternateName,
WarehouseSubType.name AS WarehouseSubTypename,
Warehouse.alternateName AS WarehousealternateName,
WarehouseSubType.alternateName AS WarehouseSubTypealternateName,
WarehouseSubType1.name AS WarehouseSubType1name,
WarehouseSubType1.alternateName AS WarehouseSubType1alternateName,
Warehouse.alternateName AS Warehousename,
Branch.name AS Branchname,
Branch.alternateName AS BranchalternateName,
WarehouseProductQuantity.actualQuantity,
WarehouseProductQuantity.reservedQuantity,
Supplier.companyName,
Supplier.companyNameAlternate,
Tafsil.description,
Tafsil.alternateDescription,
(WarehouseProductQuantity.actualQuantity - WarehouseProductQuantity.reservedQuantity) AS quantity
FROM WarehouseSubType
INNER JOIN WarehouseType ON (WarehouseSubType.warehouseTypeId = WarehouseType.id)
INNER JOIN WarehouseSubType1 ON (WarehouseSubType.id = WarehouseSubType1.warehouseSubTypeId)
) a
WHERE
warehouseTypename like '%Ve%'
AND row > 0 and row < 75
The code you posted should not even compile, let alone return rows. The sub query is not closed and row would not be recognised in the inner query.
Try this instead:
SELECT * FROM
( SELECT ROW_NUMBER() OVER (ORDER BY WarehouseSubType.id) as row,
WarehouseSubType.id,
WarehouseType.name as WarehouseTypename,
WarehouseType.alternateName AS WarehouseTypealternateName,
WarehouseSubType.name AS WarehouseSubTypename,
Warehouse.alternateName AS WarehousealternateName,
WarehouseSubType.alternateName AS WarehouseSubTypealternateName,
WarehouseSubType1.name AS WarehouseSubType1name,
WarehouseSubType1.alternateName AS WarehouseSubType1alternateName,
Warehouse.alternateName AS Warehousename,
Branch.name AS Branchname,
Branch.alternateName AS BranchalternateName,
WarehouseProductQuantity.actualQuantity,
WarehouseProductQuantity.reservedQuantity,
Supplier.companyName,
Supplier.companyNameAlternate,
Tafsil.description,
Tafsil.alternateDescription,
(WarehouseProductQuantity.actualQuantity - WarehouseProductQuantity.reservedQuantity) AS quantity
FROM WarehouseSubType
INNER JOIN WarehouseType
ON ( WarehouseSubType.warehouseTypeId = WarehouseType.id)
INNER JOIN WarehouseSubType1
ON (WarehouseSubType.id = WarehouseSubType1.warehouseSubTypeId)) a
WHERE warehouseTypename like '%Ve%' ) b
WHERE b.row > 0 and b.row< 75