JOIN in combination with ORDER BY - mysql

I want to find the earliest date in a table. How do I put this into a join statement:
SELECT date FROM table1 WHERE orderno = 222 ORDER BY date LIMIT 1
Orderno 222 can have 1-* many rows in table1 that's why I'm using LIMIT 1
I have table these tables:
Order
OrderLine
ProductionDate
Order can have 1-* ProductionDates so when i do my join of ProductionDate i want to find the earliest date.
So my guess of a sql statement would be something like:
SELECT * FROM Order
LEFT JOIN (
IN SELECT date FROM ProductionDate ORDER BY date ASC LIMIT 1)
but this doesn't work. And i would like to know what i should change to get it to work?

I'm not sure if MySQL supports a LIMIT In a derived table, but if it does, the syntax should be:
SELECT ord.*
FROM `Order` ord
LEFT JOIN (SELECT date FROM ProductionDate ORDER BY date ASC LIMIT 1) t
ON t.date = ord.date
Note that order is a reserved word, so you will need to quote it properly. Either using MySQL dreaded backticks (see above) or - if you configured your server to be standard's compliant - using double quotest like this: "order"

SELECT `date`
FROM `table1`
INNER JOIN `table1`.`date` ON `table2`.`date`
ORDER BY date ASC LIMIT 1
Is that what you mean?

Related

MySQL Subquery from ASC order to DESC order

select *, sum(sales_qty) as total_qty
from sales_details
left join sales on sales.salesid=sales_details.salesid
where month(sales.sales_date)='$m'
group by productid order by total_qty asc limit 2, 4
I have that sql that is sorted in ascending order. Now I want the results to be sorted in descending order. I have tried using subquery but doesn't work for me. Any help or suggestion is appreciated.
This is the sql that I have tried:
select * from (
select *, sum(sales_qty) as total_qty
from sales_details
left join sales on sales.salesid=sales_details.salesid
where month(sales.sales_date)='$m'
group by productid
order by total_qty asc
limit 2, 4
) as sub
order by sum(sales_qty) desc
Your query with the subquery should end
... ORDER BY sales_qty DESC
Why? When you say ... ORDER BY SUM(sales_qty) DESC you're converting the outer query into an aggregate query. Because that outer aggregate query has no GROUP BY clause, it necessarily has a one row result set.
The outer query treats the inner query as if it were a table, a virtual table. That virtual table has a column named sales_qty. It is the value by which you want to order.
Pro tip: Don't use SELECT * in aggregate queries. You're misusing the notorious nonstandard MySQL extension to GROUP BY, which means your result set contains unpredictable values. Read this. https://dev.mysql.com/doc/refman/5.7/en/group-by-handling.html

Delete only one record in mysql database

what is the proper way to delete only one record from mysql data base.
this query used for select only one record.
SELECT * FROM Customers WHERE Country='Mexico' ORDER BY Country ASC LIMIT 1;
the above query run correctly .but when replace select to delete that not worked.
DELETE FROM Customers WHERE Country='Mexico' ORDER BY Country ASC LIMIT 1;
how I can fix it?
If you have an id column you can use a subselect. I have removed the order by since this will be the same like order by 'Mexico' asc which is pretty useless.
DELETE FROM Customers
WHERE (CustomerID IN ( SELECT CustomerID
FROM Customers where country = 'Mexico'
ORDER BY CustomerID ASC LIMIT 1 )) ;
I think below query will help you. You will need to have some key ID to differentiate.
DELETE FROM Customers
WHERE SOME_KEY_ID IN
(
SELECT SOME_RANDOM_ID FROM
(
SELECT B.SOME_KEY_ID SOME_RANDOM_ID FROM Customers as B
where Country = 'Mexico'
LIMIT 1
) as c
) ;
Note: The inner select SOME_RANDOM_ID is required, else sqlfiddle throws errors This version of MySQL doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery': .
Reference FIDDLE Here

mysql query select * from table where image!='' order by date and with different upload user

I want to make some mysql query, query out 5 image order by date, but from 5 different upload user.
mysql datatable Structure: id, user_id, image, text, date
SELECT *
FROM mytable
WHERE image!=''
GROUP BY user_id
ORDER BY date DESC
LIMIT 5
But this GROUP BY always cause get the user_id earlier upload image then make a ORDER BY date DESC, I need the fresh 5 images which uploaded by 5 differents users. Maybe should use some UNION, ask for a help, thanks.
Use INNER JOIN with MAX
SELECT
mytable.*
FROM
mytable
INNER JOIN
(SELECT MAX(id) AS ID, id FROM mytable GROUP BY user_id) AS m
ON m.id = mytable.id
WHERE image != ''
GROUP BY user_id
ORDER BY DATE DESC
LIMIT 5
This will fetch the max(latest) id and integrate it to the outer select
Try this query
SELECT *
FROM temp
WHERE image!=''
GROUP BY user_id
HAVING date=max(date)
LIMIT 5
Not sure about mysql but this works in sybase.

can this sql query be simplified?

I have 2 tables. MySql
users : A_ID,name
event : B_ID, A_ID,cat_id,date
Now I want to get all users , who participated more at events on a given period of time, and need to add based on category too. I am doing something like this :
select name from users ,
(select A_id, count(*)
from event
where date<=givendate
group by A_id
order by count(*) desc ) e
where users.A_id=e.a_id
limit 0,5
Is there any easy and prof way to write that script ?
Thanks
Your query looks OK apart from a few minor points:
Your ORDER BY and LIMIT should be in the outer select otherwise the order of the results is indeterminate.
Use the JOIN keyword to join two tables.
Try this:
SELECT u.name
FROM users AS u
JOIN
(
SELECT A_id, COUNT(*) AS cnt
FROM event
WHERE date <= givendate
AND cat_id = 42
GROUP BY A_id
) AS e
USING (A_id)
ORDER BY e.cnt DESC
LIMIT 5

sql pulling from sub query

Is it possible to pull 2 results from a sub query in a sql statement?
I have:
"SELECT
(SELECT bid FROM auction_bids WHERE itemID=a.id ORDER BY bid DESC LIMIT 1) as topbid,
a.* FROM auction_items a ORDER BY a.date DESC LIMIT 15"
The part where it returns the topbid, i'd also like it to pull not only bid (as topbid) but also date (as topdate) as well. How can I do that? Do I need another sub query or can it pull both in one?
Dependent subquery (depending on some values outside, like a.id in your case) is not a very efficient way to find maximum values in subsets.
Instead use a subquery with GROUP BY:
SELECT b.topbid, b.topdate, a.*
FROM auction_items a
LEFT JOIN
( SELECT itemID, MAX(bid) as topbid, MAX(date) as topdate
FROM auction_bids
GROUP BY itemID ) b
ON a.id = b.itemID
ORDER BY a.date DESC
LIMIT 15