Can anyone advise on what im doing wrong here?
I am trying to select a table full of results, some are duplicates, but the timestamp is different, so I want to filter out by the latest date.
I'm using the following sql query but it just keeps throwing up errors?
$sql = "(SELECT *
FROM measrate
WHERE TRANS_TIME = (SELECT MAX(TRANS_TIME) FROM measrate)";
First, the initial parenthesis should be unnecessary:
SELECT m.*
FROM measrate m
WHERE TRANS_TIME = (SELECT MAX(TRANS_TIME) FROM measrate m2);
Second, if you want only the max time for some group -- say based on the column result -- then modify this to be a correlated subquery:
SELECT m.*
FROM measrate m
WHERE TRANS_TIME = (SELECT MAX(TRANS_TIME) FROM measrate m2 WHERE m2.result= m.result);
sql = "(SELECT * FROM measrate WHERE TRANS_TIME = (SELECT MAX(TRANS_TIME) FROM measrate))";
Forgot last bracket
Related
This is my table structure
and this is my dataset
What I want is query that gets data ordered by date desc and group by id_patient
so the result in the dataset example should be like this:
I would go with limit clause with subquery since you have PK :
select *
from table t
where id = (select t1.id
from table t1
where t1.id_patient = t.id_patient
order by t1.date desc
limit 1
);
However, if single patient has multiple same dates then this would produce only single records based on date.
SELECT * from rdv a JOIN (SELECT id_patient,MAX(date) date FROM rdv GROUP by id_patient ) b on a.id_patient = b.id_patient and a.date = b.date
If you want the latest record for each patient, then you are not looking for an aggregation. I would often approach this with a correlated subquery:
select t.*
from t
where t.date = (select max(t2.date) from t t2 where t2.id_patient = t.id_patient);
SELECT *
FROM table
GROUP BY group by id_patient
ordered by DATE(date) desc;
I am having trouble with this query and am hoping someone can help.
SELECT
myTable.id,
myTable.subject,
myTable.upvotes,
myTable.downvotes,
(SELECT COUNT(id)
FROM myTable
WHERE myTable.thread = myTable.id) AS comments_count
FROM myTable
Basically I have a table with posts and comments, the comments thread is tied to the id of the original post. In my query I want to show how many relpies (how many threads = id) from all rows for the current id/row.
I hope that makes sense :)
You need to specify the the table with new alias to match in your subquery other wise it will match the thread with id from same table
SELECT
m.id,
m.subject,
m.upvotes,
m.downvotes,
(SELECT COUNT(id)
FROM myTable
WHERE myTable.thread = m.id) AS comments_count
FROM myTable m
Or Better to use LEFT JOIN
SELECT
m.id,
m.subject,
m.upvotes,
m.downvotes,
COUNT(mm.id) AS comments_count
FROM myTable m
LEFT JOIN myTable mm ON(mm.thread = m.id)
GROUP BY m.id
I am trying to modify a query which results in 2 records before the modification for some reason my modification makes it not work as it return nothing.
This Query works and returns 2 record:
$query = mysql_query("SELECT * FROM `table1`
WHERE `date` = '{$eventdate->format('Y-m-d')}'
OR `date` >= CURDATE() ORDER BY id DESC");
...the I added this: JOIN table2 USING(id)
...so this final code is this:
$query = mysql_query("SELECT * FROM `table1` JOIN `table2` USING(id)
WHERE `date` = '{$eventdate->format('Y-m-d')}'
OR `date` >= CURDATE() ORDER BY id DESC");
Problem is the second one returns nothing.
Is this a syntax error? How can I get this to work? Both tables have id fields.
Make sure that table2 contains matched data, where the id is equal to the id in table1.
You can use a LEFT JOIN if this match is not required.
id in your ORDER BY is now ambiguous. There might be more errors though. Check with mysql_error()
Try This
$query = mysql_query("SELECT * FROM `table1` a,`table2` b WHERE a.id=b.id
and (`a.date` = '{$eventdate->format('Y-m-d')}' OR `a.date` >= CURDATE())
ORDER BY id DESC")
When joining two tables which have no prefixes on the column names like - table1_id, table2_id, you should use aliases like -
SELECT * FROM table1 as t1 JOIN table2 as t2 on ...
and then you can refer to the fields in the table like this - t1.id, t2.id (you can do this also without aliases( as t1) and then you should refer to the fields like - table1.id).
The problem with your script is that the 2 tables have column id and in :
ORDER BY id DESC
the engine doesn`t know from which table do you refer this id
Other suggestion of mine is when possible not to use aggregation functions in the queries(in your query the CURDATE is that type of function). Aggregation functions in SQL prevent query caching. In our case you can pass the currdate from php to the query and the query can be cached.
Hope i`ve helped.
I have a bit of a situation here.
I have a query:
SELECT DISTINCT (testrecurring.id), testrecurring.cxl, testci.cd
FROM testci, testrecurring
WHERE (testci.id = testrecurring.id)
AND testci.x_origin='1'
ORDER BY testrecurring.id DESC;
Now, if a var is not set, I need to do a select on this query, and here is the catch. I need to exclude some id's. Here is how I'm doing it now.
I create a table with that query: create table xxx SELECT * ..... and now the results from my previous query are inside another table called xxx.
Then:
if (!isset($var)) {
$delete = mysql_query("delete from xxx USING xxx, future_recurring where xxx.id = future_recurring.id");
}
and after the records have been deleted I do my final select * from xxx.
This works just fine, the only thing is that I need to redo all this logic by not creating any tables. Maybe doing some joins, I'm not sure how to proceed.
I hope this is not very confusing.
Any ideas?
And now how about this?:
SELECT tr.id, tr.cxl, tci.cd
FROM testci AS tci
INNER JOIN testrecurring AS tr
ON tci.id = tr.id
LEFT OUTER JOIN future_recurring AS fr
ON tr.id = fr.id
WHERE tci.x_origin='1'
AND fr.id IS NULL
GROUP BY tr.id, tr.cxl, tci.cd
ORDER BY tr.id DESC
This only includes results in which the testrecurring.id is NOT FOUND in future_recurring
You just need to add a where condition to exclude the rows you don't want:
SELECT *
FROM testci
JOIN testrecurring on testrecurring.id = testci.id
WHERE testci.x_origin='1'
AND testci.id NOT IN (SELECT id FROM future_recurring)
ORDER BY testrecurring.id DESC;
Or you could try this which might give better performance:
SELECT *
FROM testci
JOIN testrecurring on testrecurring.id = testci.id
LEFT JOIN future_recurring on future_recurring.id = testci.id
WHERE testci.x_origin='1'
AND future_recurring id IS null
ORDER BY testrecurring.id DESC;
Although, if your indexes are good and sane and your data sets aren't enormous then the performance should be close.
I'm trying to simplify some mysql. I have three selects and three tables:
SELECT pageid FROM thepage WHERE DATE(createdate)='2011-11-09' ORDER BY createdate DESC
SELECT urlid FROM themix WHERE pageid=...
SELECT link FROM theurl WHERE urlid=...
Given a createdate, it gets all pageid's, then goes to a second table and gets all urlid's for those pageid's then gets the links for all those urlid's. I'm using while loops right now, but suspect there is a way to join them all into one.
Any ideas?
SELECT p.pageid
, u.link
FROM thepage p INNER JOIN themix m
ON p.pageid = m.pageid
INNER JOIN theurl u
ON m.urlid = u.urlid
WHERE DATE(p.createdate) = '2011-11-09'
ORDER BY p.createdate DESC
should do the trick i think. If you want only unique URLs, use this:
SELECT u.link
FROM thepage p
INNER JOIN themix m
ON p.pageid = m.pageid
INNER JOIN theurl u
ON m.urlid = u.urlid
WHERE DATE(p.createdate) = '2011-11-09'
GROUP BY m.urlid
ORDER BY MIN(p.createdate) DESC
Sidenotes
WHERE DATE(p.createdate) = '2011-11-09'
will have to scan the whole page table and call the DATE() function for every row. You have two options to make it faster:
Change the createdate to DATE (from DATETIME or TIMESTAMP, that it is now) and use:
WHERE p.createdate = '2011-11-09'
or keep it as it is and use:
WHERE p.createdate >= '2011-11-09'
AND p.createdate < '2011-11-10'
Table and column names are better without prefixes or suffixes like the the you have. Isn't this cleaner and clearer (even without the aliases)?
SELECT url.link
FROM page
INNER JOIN mix
ON page.pageid = mix.pageid
INNER JOIN url
ON mix.urlid = url.urlid
WHERE DATE(page.createdate) = '2011-11-09'
ORDER BY page.createdate DESC
you already received some feedback in the comments to your post. you should really learn about the different types of sql joins
what you want should be achieved by something like this:
SELECT
theurl.link
FROM
thepage
LEFT JOIN
themix
ON
thepage.pageid = urlid.pageid
LEFT JOIN
theurl
ON
theurl.urlid = themix.urlid
WHERE
DATE(thepage.createdate)='2011-11-09'
ORDER BY
thepage.createdate DESC
you should use the answers you got here as a starting point for your study on sql
SELECT link from theurl where (select urlid from themix where (select
pageid from thepage where WHERE DATE(createdate)='2011-11-09' ORDER BY
createdate DESC))
OR
SELECT link from theurl as tu , themix as tm, thepage as tp where
tu.id=tm.urlid and tm.pageid=tp.id and DATE(createdate)='2011-11-09'
ORDER BY
createdate DESC
Any way it will give result.
You can nest the 3 queries into 1 and use the IN-expression to search for multiple id's.