I currently have the following queries (and some surrounding vB code). I was hoping I could slim this up into a single SELECT statement versus having to run 10 more on each page to get that names of individuals.
$results = $db->query_read_slave("
SELECT user.id AS steam, user.skills AS level
FROM wcsp.wcgousers AS user
WHERE race = '_wcs_'
ORDER BY ABS(user.skills) DESC LIMIT 10
");
$rank = 1;
while ($user = $db->fetch_array($results)) {
$result = $db->query_first("
SELECT old.name AS name
FROM wcsp.warn_oldnames AS old
WHERE id = '$user[steam]'
ORDER BY lasttime
DESC LIMIT 1
");
$listing[] = array("id" => $user['steam'], "level" => $user['level'], "name" => $result['name'], "rank" => $rank);
$rank += 1;
}
I have tried LEFT JOIN but the issue I run into is that I would need a subquery in the LEFT JOIN similar to:
SELECT user.id AS steam, user.skills AS level, names.name AS name
FROM wcsp.wcgousers AS users
LEFT JOIN
(
SELECT names.name
FROM wcsp.warn_oldnames AS names
WHERE id = wcsp.wcgousers.id
ORDER BY lasttime DESC LIMIT 1
) AS names
ON names.id = users.id
WHERE users.race = '_wcs_'
Which won't work due to the different database check inside the subquery.
If I understand you correctly, you want to get the latest Name for every users.
SELECT a.id AS steam,
a.skills AS level,
b.name
FROM wcgousers a
INNER JOIN warn_oldnames b
ON a.ID = b.ID
INNER JOIN
(
SELECT ID, MAX(lasttime) max_date
FROM warn_oldnames
GROUP BY ID
) c ON b.ID = c.ID AND
b.lastTime = c.max_date
WHERE a.race = '_wcs_'
-- ORDER BY ABS(a.skills) DESC
-- LIMIT 10
Related
I have tow tables, first table related to forms and second table relate to adviser.each adviser can add comment for a form.
I tried select some column of two tables. It was works i have last subject of second table (adviser table ) in my result .but i need first subject of second table(adviser table ) .
#DQL
SELECT
f.name,
f.title,
f.conditionResultFinal,
f.conditionResult,
f.formCode,
f.dateInsert,
f.id,
(a.idFormRequestProject),
a.subject as subjectAdvisor
FROM AdminBundle:FormRequestProject f
JOIN AdminBundle:Advisor a
WHERE a.idFormRequestProject = f.id
AND (f.conditionResultFinal = 0 OR f.conditionResult = 0)
AND f.displayStatus = 1
GROUP BY f.id
ORDER by a.id,f.id DESC
Finally i solve this problem .i deleted GROUP By .and add new where ...
SELECT f.name,f.title,f.conditionResultFinal,
f.conditionResult ,
f.formCode,f.dateInsert ,
f.id,(a.idFormRequestProject),
a.subject as subjectAdvisor
FROM AdminBundle:FormRequestProject f
Left JOIN AdminBundle:Advisor a WHERE f.id = a.idFormRequestProject AND a.id =
(SELECT Max (aa.id) FROM AdminBundle:Advisor aa WHERE a.idFormRequestProject =
aa.idFormRequestProject ORDER by aa.id ASC ) AND
(f.conditionResultFinal = 0 OR f.conditionResult = 0 ) AND f.displayStatus =1
ORDER by f.id DESC
I have a simple mysql query:
SELECT U.id FROM
users U
INNER JOIN friends F
ON ( U.id = F.id_exp AND F.id_des = :id_exp )
OR ( U.id = F.id_des AND F.id_exp = :id_exp ) WHERE
U.id <> :id_des
AND F.active = 1
I wish that by calling this request again, that It does not return the id that It has already previously returned.
I hope you understand me.
Thank you.
You must give RAND a seed number in order to always get the same order. This can be any number, e.g. 1:
...
ORDER BY RAND(1)
LIMIT #offset, 1;
I want get some of data with one distinct email.
My code is below:
SELECT DISTINCT Email,
Degree,
Majority
FROM job_education a
WHERE a.Status = 'Active'
AND a.IdEducationLevel = (
SELECT Max(b.IdEducationLevel)
FROM job_education b
WHERE b.Status='Active'
AND a.Email = b.Email
)
Thanks your kindness before
Your code should be fine, although I would fix the table aliases:
SELECT je.*
FROM job_education je
WHERE je.Status = 'Active' AND
je.IdEducationLevel = (SELECT MAX(je2.IdEducationLevel)
FROM job_education je2
WHERE je2.Status = 'Active' AND je2.Email = je.Email
);
If you are getting multiple rows for a given email -- and you want only one row -- then use a better id in the subquery:
SELECT je.*
FROM job_education je
WHERE je.Status = 'Active' AND
je.job_education_id = (SELECT je2.job_education_id
FROM job_education je2
WHERE je2.Status = 'Active' AND je2.Email = je.Email
ORDER BY IdEducationLevel DESC, job_education_id DESC
LIMIT 1
);
I have invented an id called job_education_id for this purpose. Note that this query uses ORDER BY and LIMIT rather than an aggregation function.
you should use a join
(the subselect can't see the upper reference to the table a)
SELECT DISTINCT Email,
Degree,
Majority
FROM job_education a
INNER JOIN (
SELECT b.Email, Max(b.IdEducationLevel) max_IdEducationLevel
FROM job_education b
WHERE b.Status='Active'
GROUP BY b.Email
) t ON a.Email = t.Email and a.IdEducationLevel = t.maxy_IdEducationLevel
WHERE a.Status = 'Active'
In relation to the answer I accepted for this post, SQL Group By and Limit issue, I need to figure out how to create that query using SQLAlchemy. For reference, the query I need to run is:
SELECT t.id, t.creation_time, c.id, c.creation_time
FROM (SELECT id, creation_time
FROM thread
ORDER BY creation_time DESC
LIMIT 5
) t
LEFT OUTER JOIN comment c ON c.thread_id = t.id
WHERE 3 >= (SELECT COUNT(1)
FROM comment c2
WHERE c.thread_id = c2.thread_id
AND c.creation_time <= c2.creation_time
)
I have the first half of the query, but I am struggling with the syntax for the WHERE clause and how to combine it with the JOIN. Any one have any suggestions?
Thanks!
EDIT: First attempt seems to mess up around the .filter() call:
c = aliased(Comment)
c2 = aliased(Comment)
subq = db.session.query(Thread.id).filter_by(topic_id=122098).order_by(Thread.creation_time.desc()).limit(2).offset(2).subquery('t')
subq2 = db.session.query(func.count(1).label("count")).filter(c.id==c2.id).subquery('z')
q = db.session.query(subq.c.id, c.id).outerjoin(c, c.thread_id==subq.c.id).filter(3 >= subq2.c.count)
this generates the following SQL:
SELECT t.id AS t_id, comment_1.id AS comment_1_id
FROM (SELECT count(1) AS count
FROM comment AS comment_1, comment AS comment_2
WHERE comment_1.id = comment_2.id) AS z, (SELECT thread.id AS id
FROM thread
WHERE thread.topic_id = :topic_id ORDER BY thread.creation_time DESC
LIMIT 2 OFFSET 2) AS t LEFT OUTER JOIN comment AS comment_1 ON comment_1.thread_id = t.id
WHERE z.count <= 3
Notice the sub-query ordering is incorrect, and subq2 somehow is selecting from comment twice. Manually fixing that gives the right results, I am just unsure of how to get SQLAlchemy to get it right.
Try this:
c = db.aliased(Comment, name='c')
c2 = db.aliased(Comment, name='c2')
sq = (db.session
.query(Thread.id, Thread.creation_time)
.order_by(Thread.creation_time.desc())
.limit(5)
).subquery(name='t')
sq2 = (
db.session.query(db.func.count(1))
.select_from(c2)
.filter(c.thread_id == c2.thread_id)
.filter(c.creation_time <= c2.creation_time)
.correlate(c)
.as_scalar()
)
q = (db.session
.query(
sq.c.id, sq.c.creation_time,
c.id, c.creation_time,
)
.outerjoin(c, c.thread_id == sq.c.id)
.filter(3 >= sq2)
)
I'm struggleing to get the following to work:
# prev
SELECT klha.buyerID < PARAMETER_1
FROM agents AS v
LEFT JOIN seller_authorized AS klhs
ON klhs.agent= v.agentID
AND klhs.iln = v.seller
AND v.`status` = 'auth'
LEFT JOIN cust_list AS klha
ON klha.buyerID = klhs.userID
AND klha.accountNo = klhs.accountNo
WHERE v.iln = PARAMETER_2
AND klhs.acccountNo IS NOT NULL
ORDER BY klha.buyerID
LIMIT 1
;
END
The left joins are working as I'm using them in another procedure to select ALL relevent records. The problem is with my attempt to select the previous record.
I'm passing in a buyerID and need to get back the id of the previous record.
Question:
Can anyone point me to the correct syntax?
EDIT:
If I run this in MySQL, I'm getting "0" as resultset
Thanks!
SOLUTION:
Got it to work like this:
# prev
SELECT klha.buyerID
FROM agents AS v
LEFT JOIN seller_authorized AS klhs
ON klhs.agent= v.agentID
AND klhs.iln = v.seller
AND v.`status` = 'auth'
LEFT JOIN cust_list AS klha
ON klha.buyerID = klhs.userID
AND klha.accountNo = klhs.accountNo
WHERE v.iln = PARAMETER_2
AND klhs.acccountNo IS NOT NULL
AND klha.buyerID < PARAMETER_1
ORDER BY klha.buyerID
LIMIT 1
;
Easier than thought :-) Thanks #Henrique Ordine
If I understand correctly what you need, adding this condition to your where clause should do the trick:
and klha.buyerID = (select max(buyerID) from agents where buyerID < PARAMETER_1)
Like this:
SELECT klha.buyerID
FROM agents AS v
LEFT JOIN seller_authorized AS klhs
ON klhs.agent= v.agentID
AND klhs.iln = v.seller
AND v.`status` = 'auth'
LEFT JOIN cust_list AS klha
ON klha.buyerID = klhs.userID
AND klha.accountNo = klhs.accountNo
WHERE v.iln = PARAMETER_2
AND klhs.acccountNo IS NOT NULL
and klha.buyerID = (select max(buyerID) from agents
where buyerID < PARAMETER_1)
ORDER BY klha.buyerID
LIMIT 1