Using a Select query inside a select query? - php

I am trying to use a select query within another select query in the from clause.This is a mysql query.
SELECT invoiceNo, SUM(weight), COUNT(barcode), sum_total
FROM (SELECT * FROM `selected_items` WHERE date LIKE '07-Jan-2016' ORDER BY id DESC)
WHERE date LIKE '07-Jan-2016' GROUP BY invoiceNo;
This is how my database looks like.
How can I achieve something like this where I have a table which I order by id desc and use another select query on the result of this query.The above query gives me an error #1248 - Every derived table must have its own alias.Can anyone help I am new to programming.

I hope this will help you out .
SELECT invoiceNo, SUM( weight ) , COUNT( barcode ) , sum_total
FROM (SELECT * FROM `selected_items`
WHERE DATE LIKE '07-Jan-2016'
ORDER BY id DESC
)a
GROUP BY invoiceNo;

I didn't get it why you are using sub-query. You can directly use the table 'selected_items' and check the condition in where clause.Like:
SELECT invoiceNo, SUM(weight), COUNT(barcode)
from 'selected_items'
WHERE date LIKE '07-Jan-2016'
GROUP BY invoiceNo
Order By Id

You dont need to do order by inside inner select query. And in MySQL, you need to give an alias to a derived table, a in this example.
SELECT invoiceNo, SUM(weight), COUNT(barcode), sum_total
FROM
(
SELECT * FROM `selected_items` WHERE date LIKE '07-Jan-2016'
) a
WHERE date LIKE '07-Jan-2016' GROUP BY invoiceNo;
Without looking at data, it is difficult to decide to order by but this query will work for you.

You're query is almost right. However there's only missing. You must used an ALIAS.
SELECT * FROM
(
SELECT * FROM #table
)AS tbl1 WHERE field1 = 'test'

Related

query group by with order by

i need to create query with group by and order by, and i dont know how to do it.
query should return one record for the newest date for existing device_serial_number. enter image description here
so i would to get id 591 nad 592
solution can be in sql or the best way it will be in symfony, through query builder etc.
There are many ways to accomplish what you want.
First Way
The oldest way to select first, best, worst, whatever within a group is with a correlated subquery:
Select * from mytable outer
Where created_at = (
Select max(created_at)
from mytable inner
Where inner.device_serial_number = outer.device_serial_number
)
Second Way
Use a subselect to find earliest dates for all devices, them join back to the original table to filter:
Select a.*
From mytable a Inner Join
(Select device_serial_number, max(created_at) as latedate
From mytable b
Group By device_serial_number
) b
On a.device_serial_number=b.device_serial_number
And a.created_at=b.latedate
Third way
Use a window function to rank order all the dates and then pick the number one ranking.
Select * From (
Select *
, rank() Over (Partition By device_serial_number Order by created_at desc) as myrank
From mytable
)
Where myrank=1
Notice that while these 3 solutions use different aspects of SQL, they all have a common analytical approach. They are all two step processes whose first (inner) part involves finding the most recent created_at date for each device_serial_number and then reapplying that result back to the original table in the second (outer) part.

Mysql - Order By in IN Clausole

I have a simple question.
I have to join multiple queries and most of them has ad IN clausole.
Unfortunally, MySQL doesn't allow ORDER BY in an UNION (only outside all unions) but I need a specific ordering that I can't get with an outside order by.
Question is:
If I have a IN clausole like
foo in ('newyork','boston','atlanta')
may I assume MySql's engine will order the resulting rows by IN position (so, first all the row with foo = 'newyork' then 'boston' etc...
Thanks.
No. It's nothing like that. You need to explicitly add an order by clause.
If you want to order your rows based on position in a set, you can use field function.
order by field(foo,'newyork','boston','atlanta')
Select *
from
(
Select * from Temp1
order by x
) Tbl1
UNION
Select *
from
(
Select * from Temp2
order by y
) Tbl2
Try this.

MySQL order results by date after a group by

We are working on a project for school and have a small issue with a query.
What we try to do is the following:
Select the education-unit(s) with the same version_vid and after that select the education-unit with the latest version_date.
But whatever we try, the education-unit with the lowest euid is returning.
We are using the Yii2 framework for this project, the ActiveQuery we use is:
EducationUnit::find()->groupBy('version_vid')->orderBy('version_date DESC');
SQL Fiddle:
http://sqlfiddle.com/#!9/9929d/2/0
Thanks in advance!
Maybe this can help you:
EducationUnit::find()
->from('select * from education_unit ORDER BY version_date DESC')
->groupBy('version_vid')
->all();
Wrap a select around your query, after that do the group by:
SELECT * FROM
( SELECT * FROM `education_unit` ORDER BY `version_date` DESC ) a
GROUP BY a.`version_vid`
Why it didn't work in your query is because SQL has an execution plan as below:
FROM clause
WHERE clause
GROUP BY clause
HAVING clause
SELECT clause
ORDER BY clause
The reason you are getting the lowesteuid is that the ORDER BY is applied after the GROUP BY and the GROUP BY is choosing arbitrary values from the group to return.
This is a classic top in group question and has been answered previously
I personally like the answer supplied by Bill Karwin which when applied to your situation becomes:
SELECT t.*
FROM table t
LEFT JOIN table t2
ON t2.version_vid = t.version_vid
AND t2.version_date > t.version_date
WHERE t2.version_vid IS NULL
Another common solution is:
SELECT t.*
FROM table t
JOIN (
SELECT version_id, MAX(version_date) version_date
FROM table
GROUP BY version_id
) t2
ON t2.version_id = t1.version_id
AND t2.version_date = t1.version_date

Select rows from two different tables but order by date using php and MySQL

I have two tables;
I'm trying to produce a statement (like a bank statement) to show all charges and all payments. The charges and payments need to be displayed in date order based on the statement_date field.
I'm pretty sure I need to use 'unions' but I just can't seem to find a solution.
My effort so far is;
$statement_sql = "(SELECT * FROM accounts_tenant_charge)
UNION
(SELECT * FROM accounts_tenant_payment)
ORDER BY statement_date";
Thanks in advance.
Thanks for your help. The following query worked for me;
SELECT tenant_charge_date as statement_date, tenant_charge_id as reference, tenant_charge_total_amount as debit, NULL as credit, 'Charge' as type FROM accounts_tenant_charge
UNION ALL
SELECT tenant_payment_date as statement_date, tenant_payment_id as reference, NULL as debit, tenant_payment_amount as credit, 'Payment' as type FROM accounts_tenant_payment
ORDER BY statement_date
You must be make nested query. Firstly, you make union query based on your tables and put in inside query. Then, you make SELECT query and put in outside. Don't forget to give alias table for outside query.
You can try this code :
SELECT * FROM
(
(SELECT * FROM accounts_tenant_charge)
UNION
(SELECT * FROM accounts_tenant_payment)
) a
ORDER BY a.statement_date ASC
Your tables have different number of columns, therefore you cant union them right away by doing a select * . You have to select an equal number of columns from both tables. Like:
select
tenant_charge_id as `id`,
tenant_charge_date as `date`,
tenant_charge_total_amonunt as `amount`
union
select
tenant_payment_id,
tenant_payment_date,
tenant_payment_amount
order by
`date` asc

Mysql SELECT statement order by count

I am not sure if this is possible or not
I have a table with orders and userid
I need to do a SELECT query with ORDER BY the least number of orders of a customer
Example:
userid1 has 10 orders
userid2 has 5 orders
Then SELECT should show userid2 at the top
Thanks in advance
If you have orders as ordersCount i mean as a field which keeps counts of orders then
Select * FROM YourTable order by orders ASC
Else if you have a record for each order then
Select * FROM YourTable Group by UserID order by count(*) ASC;
Apply limit like this
Select *
FROM YourTable
Group by UserID
order by count(*) ASC
limit startIndex, fetchSize;
where startIndex and fetchSize are int that define your limit
You need to group by the userid so you can count orders by user.
SELECT userid, count(1) FROM orders GROUP BY userid ORDER BY count(1) asc
You can accomplish that using GROUP BY and ordering by COUNT().
It would be something like that.
SELECT userid, COUNT(*) qt
FROM orders
GROUP BY userid
ORDER BY qt
If each order is in its own row, however, you need to aggregate them:
SELECT * from MYTABLENAME GROUP BY userid ORDER BY COUNT(*)
Count is what you're looking for
SELECT count(theField) as theCount, *
FROM theTable
GROUP BY theField
ORDER BY theCount
You can select the COUNT of a column and give it a name, then use that name to sort by. Here's an example:
SELECT
p.*,
(SELECT COUNT(o.order_id) FROM orders AS o WHERE p.partron_id=p.patron_id) AS orderCount
FROM
patrons AS p
WHERE
...
ORDER BY
orderCount ASC
UPDATE: Just realized this is only useful if you have orders in a separate table than patrons. Disregard if you are only looking at a single table.
This query expects you to have two tables, a patron table with patron information like name, and a orders table that has a patron_id column that matches the identity column in the patron table.
The advantage to doing this is that you can request patron information at the same time, so you don't need to run two queries in PHP if you're going to use that information.

Categories