Do a while in a query, subquery - mysql

I want do query that show data of some users bat, first is a other query, is something difficult of explain. i dont want get the data of the users that me follow, i want see data that me don't follow. So i have two table "follow_follower", "user" and now do a middle query but doesn't match:
follow_follower
cod seguidor seguido
1 1 2
2 1 3
3 1 8
4 1 6
5 8 2
6 2 8
7 2 4
8 3 2
9 5 1
User
cod nombre apellido telefono
1 carlos cardenas 12587
2 Umberto Contreras 125488
3 carlos Torres 44587
4 Victor Sambrano 69754
5 Carlos Barragan 3698741
6 Jorge Cantor
7 Umberto Zanetty 578825
8 Miguel Cantor 5488787
ok and if you see are four fields in follow_follower that the user is 1 (Carlos Cardenas), how do query that first get all user that me follow and after select all data of people diferent "DISTICN" see my query:
select distinct(nombre), apellido, telefono
FROM usuario
where cod<> ANY(select seguido from follow_follower where seguidor=1)
order by cod
But only DISTINC a ANY row of all query.
ok i want a list little of people that me don't follow but i see your query and show error, i know little of mysql query ADVANCE :( and the other problem is that my db is write in spanish... but go to try the table db like are!!! :
this is a the true query:
SELECT distinct(usua.cod), usua.nick, desi.usuario, main.dir, main.size, main.x, main.y, main.R, main.G, main.B, main.A
FROM usuario AS usua, design AS desi, mainPhoto AS main
WHERE desi.usuario = usua.cod
AND desi.cod = main.design
AND usua.cod <> ANY(select seguido from seguidor_seguido where seguidor=1)/**!!!!!/
AND main.dir <> ''
AND main.type =1
ORDER BY usua.nick DESC
LIMIT 44 , 6
so the unique table that true import are usuario (content data of user) and seguidor_seguido(content all the records follow)
---describe table seguidor_seguido:
seguidor_seguido
cod // is the id of the record
seguidor // is the user that follow to other users
seguido // is the user that is followed by other users
Although the request is different from the original is the same problem!!!
thanks :D

Sounds like you want a list of all users that don't follow you. I'm not sure if you want that to be transitive or not (i.e., including users that follow users that follow you, etc.).
If you just want to do it one level deep, it'll go like this. You want to get a list of all entries in the follow_follower where the seguido (I assume that's supposed to mean "person who is being followed") is not you. Users can follow other people, but they can't follow you. You can join twice against the User table to get user names.
SELECT DISTINCT
usuario_seguidor.nombre,
usuario_seguidor.applido,
usuario_seguidor.telephono
FROM follow_follower
JOIN usuario AS usuario_seguidor
ON usuario_seguidor.cod = follow_follower.seguidor
WHERE follow_follower.seguido <> 1

I could finally end the query and match ok!! :D see:
SELECT U.cod, U.nombre, U.apellido, U.telefono FROM User U LEFT JOIN (SELECT f.seguido
FROM follow_follower f
WHERE f.seguidor = 1)t1 ON u.cod = t1.seguido
WHERE t1.seguido IS NULL AND U.cod <> 1 GROUP BY U.cod
this is the query that me need, but i thanks :D

Related

Different entries within 5 seconds

Sorry I don't know how to describe the topic.
i have a database where i store the unixtime of the entries and some other stuff, in this case the column "name" for the user and "type" it can be 1 or 2.
I want to check if there are entries where name is the same and type switches from 1 to 2 and back to 1 or 2 1 2 within 5 seconds.
So it shows me something like this:
Unixtime Name type
1550293559 Peter 2
1550293560 Peter 1
1550293561 Peter 2
Is there a query that can help me do this?
Sorry I really hope you guys understand that, I don't know how to explain the problem properly.
Thanks.
You can do that with a 3x self join on that table and the necessary conditions (All 3 rows have the same name etc.). See http://www.mysqltutorial.org/mysql-self-join/ for more info.
Note that as the join produces all the possible permutations as input material, you don't have to 'permute' the conditions in the where part of the query. E.g. To get the 5 second rule, you can just say
... where e1.unixtime > e2.unixtime and e2.unixtime > e3.unixtime and e3.unixtime+6 > e1.unixtime ...
Edit: since the original answer was downwoted, here is the full query (grumble grumble) assuming the table name 'sotest':
SELECT
*
FROM
sotest e1
JOIN
sotest e2
JOIN
sotest e3
WHERE
(e1.name = e2.name AND e2.name = e3.name
AND e1.unixtime > e2.unixtime
AND e2.unixtime > e3.unixtime
AND e3.unixtime + 6 > e1.unixtime)
AND ((e1.type = 1 AND e2.type = 2
AND e3.type = 1)
OR (e1.type = 2 AND e2.type = 1
AND e3.type = 2))

combining two select queries from the same table

I need to do something like this:
id tag status user trial Value (other columns)...
1 A Pass peter first 0
2 A Pass peter second 1
3 A Fail peter third 3
4 B Pass peter first 4
5 B Pass peter second 5
6 B Pass peter third 6
select the rows that tag equal A and status equal to Pass and find the same value for other tag ex:B
id tag status user trial Value_tag_A Value_tag_B (other columns)...
1 A Pass peter first O 4
2 A Pass peter second 1 5
I can do some processing using php to get this result, but i'm wondering if i can do it directly using sql
I've tried numerous variations and can't seem to get close to the result.
Solution: http://sqlfiddle.com/#!9/e9068d/17
I don't know why in the rows where tag=A also have Value_tag_B. I will ignore this and maybe the following query is an approach.
SELECT DISTINCT y.status, y.`user`, y.trial,
(SELECT Value FROM toto WHERE y.`user` = `user` and y.trial = trial and tag = 'A' ) AS Value_tag_A,
(SELECT Value FROM toto WHERE y.`user` = `user` and y.trial = trial and tag = 'B' ) AS Value_tag_B
FROM toto y
WHERE y.trial NOT IN (SELECT DISTINCT trial FROM toto WHERE `status` <> 'Pass')
The code has been modified.
SQL Fiddle

Joining 2 tables that use LIKE as a common identifier

I have two tables.
wp_rg_lead_detail:
id lead_id form_id field_number value
=====================================================
166649 2579 4 235 batman
167324 2602 4 235 batman
168439 2579 4 235 kelsey
169221 2836 4 235 batman
wp_rg_incomplete_submissions:
uuid form_id submission
=======================================================================
fds4389dsd2kjd 4 JSON entry that doesn't contain 'kelsey
ciwod2938slsck 4 JSON entry that contains 'kelsey
392copaa234jfl 4 JSON entry that doesn't contain 'kelsey
What I want to do is grab the record that:
has the word 'kelsey' in wp_rg_incomplete_submissions.submission
has a wp_rg_incomplete_submissions.form_id of 4
has the word 'kelsey' as a value in wp_rg_lead_detail
and the lead_id for that entry in wp_rg_lead_details should also have the word 'batman' for a value.
The only identifier between the two tables is the word 'kelsey'. But where it exists in wp_rg_lead_detail, that lead_id must also have an entry with the value of 'batman'.
I have tried subqueries and joins, and I'm getting nowhere. Can someone please point me in the right direction?
UPDATE
From the feedback below, it sounds like I should create an alias and then join them where that exists in both. Here's where I'm at:
SELECT *, 'kelsey' AS myvalue
FROM `wp_rg_lead_detail`
WHERE (`value` LIKE 'batman'
OR `value` LIKE 'kelsey')
AND `form_id` = 4
GROUP BY `lead_id`
HAVING count(*) > 1
I think somehow I need to join this where the LIKE uses myvalue:
SELECT *, uuid
FROM `wp_rg_incomplete_submissions`
WHERE `form_id` = 4
AND `submission` LIKE concat_ws(";", "%", myvalue, "%")
UPDATE #2
After continuing to struggle with this, I've come up with:
SELECT *
FROM wp_rg_lead_detail
INNER JOIN wp_rg_incomplete_submissions ON wp_rg_lead_detail.value
LIKE CONCAT('%', wp_rg_incomplete_submissions.submission, '%')
WHERE wp_rg_lead_detail.value = 'kelsey'
I know I'm doing something wrong because there are no results. But I feel it is much closer than where I started from.
So here is what I came up with, not vouching for it's efficiency as I don't write much SQL.
SELECT *
FROM submissions
JOIN (SELECT detail.*
FROM detail
JOIN detail detail2
ON detail2.lead_id = detail.lead_id
WHERE detail.value = 'kelsey'
AND detail2.value = 'batman'
) as detailjoin
ON detailjoin.form_id = submissions.form_id
WHERE submissions.submission LIKE '%kelsey%'
AND submissions.form_id = 4;
Which from you data set returns:
'ciwod2938slsck' 4 'JSON with kelsey' 168439 2579 4 235 'kelsey'
So to break it down, the inner join query gets all detail rows that have 'kelsey' as a value where that lead_id also exists in a row with a 'batman' value.
The outer query selects all rows with form_id of 4 and 'kelsey' in submission
Then it simply joins the two on form_id = form_id.
I believe this does what you needed although with the small data set not positive.

How to do this query against MySQL database table?

I was given a task to show the CPU usage trend as part of a building process which also do regression test.
Each individual test case run has a record in the table RegrCaseResult. The RegrCaseResult table looks something like this:
id projectName ProjectType returnCode startTime endTime totalMetrics
1 'first' 'someType' 16 'someTime' 'someOtherTime' 222
The RegrCaseResult.totalMetrics is a special key which links to another table called ThreadMetrics through ThreadMetrics.id.
Here is how ThreadMetrics will look like:
id componentType componentName cpuTime linkId
1 'Job Totals' 'Job Totals' 'totalTime' 34223
2 'parser1' 'parser1' 'time1' null
3 'parser2' 'generator1' 'time2' null
4 'generator1' 'generator1' 'time3' null
------------------------------------------------------
5 'Job Totals' 'Jot Totals' 'totalTime' 9899
...
The rows with the compnentName 'Job Totals' is what the totalMetrics from RegrCaseResult table will link to and the 'totalTime' is what I am really want to get given a certain projectType. The 'Job Totals' is actually a summation of the other records - in the above example, the summation of time1 through time3. The linkId at the end of table ThreadMetrics can link back to RegrCaseResult.id.
The requirements also states I should have a way to enforce the condition which only includes those projects which have a consistent return code during certain period. That's where my initial question comes from as follows:
I created the following simple table to show what I am trying to achieve:
id projectName returnCode
1 'first' 16
2 'second' 16
3 'third' 8
4 'first' 16
5 'second' 8
6 'first' 16
Basically I want to get all the projects which have a consistent returnCode no matter what the returnCode values are. In the above sample, I should only get one project which is "first". I think this would be simple but I am bad when it comes to database. Any help would be great.
I tried my best to make it clear. Hope I have achieved my goal.
Here is an easy way:
select projectname
from table t
group by projectname
having min(returncode) = max(returncode);
If the min() and max() values are the same, then all the values are the same (unless you have NULL values).
EDIT:
To keep 'third' out, you need some other rule, such as having more than one return code. So, you can do this:
select projectname
from table t
group by projectname
having min(returncode) = max(returncode) and count(*) > 1;
select projectName from projects
group by projectName having count(distinct(returnCode)) = 1)
This would also return projects which has only one entry.
How do you want to handle them?
Working example: http://www.sqlfiddle.com/#!2/e7338/8
This should do it:
SELECT COUNT(ProjectName) AS numCount, ProjectName FROM (
SELECT ProjectName FROM Foo
GROUP BY ProjectName, ReturnCode
) AS Inside
GROUP BY Inside.ProjectName
HAVING numCount = 1
This groups all the ProjectNames by their names and return codes, then selects those that only have a single return code listed.
SQLFiddle Link: http://sqlfiddle.com/#!2/c52b6/11/0
You can try something like this with Not Exists:
Select Distinct ProjectName
From Table A
Where Not Exists
(
Select 1
From Table B
Where B.ProjectName = A.ProjectName
And B.ReturnCode <> A.ReturnCode
)
I'm not sure exactly what you're selecting, so you can change the Select statement to what you need.

mysql query if condition

Hi there i have two tables a2_deal(I havent mentioned entire table as its very big)
deviceID companyID stage serverTime
1 14 -1 1349449200
1 1 -1 1349445600
2 21 -1 1349449200
3 17 -1 1349447160
1 14 3 1344449200
1 14 2 1340449200
and another table called a2_comp
companyID name
1 Microsoft
14 DELL
15 APPLE
17 Google
I am trying to get the most recent stage of a company By using below query:
SELECT deal.companyID, companies.name as Company,
if(max(serverTime),stage,Null) as Stage
FROM `a2_deal` AS deal
LEFT JOIN `a2_comp` AS companies ON deal.companyID = companies.companyID
GROUP BY companyID
ORDER BY serverTime
in my query i am using if(max(serverTime),stage,Null) as Stage which means select the stage value related to most recent server time . ie it should give me -1 as the stage of companyID 14.... But for some reason i am not getting correct output..Please explain how my logic is wrong here... Thank You
You want the groupwise maximum:
SELECT a2_comp.*, a2_deal.*
FROM a2_deal NATURAL JOIN (
SELECT companyID, MAX(serverTime) AS serverTime
FROM a2_deal
GROUP BY companyID
) t JOIN a2_comp USING (companyID)
See it on sqlfiddle.
case is used for inline conditions in your query. Also, you may need to do
(case when max(serverTime) = serverTime then stage else null end) as Stage
I'm not totally sure that's valid, but you can try it out.
Try this
SELECT deal.companyID, deal.stage, comp.name
FROM a2_deal AS deal, a2_comp AS comp
WHERE deal.serverTime =
(SELECT MAX(deal2.serverTime)
FROM a2_deal AS deal2
WHERE deal2.companyID = deal.companyID)
AND comp.companyID = deal.companyID
GROUP BY deal.companyID
This might be a little confusing but the most interesting part is the sub query which selecting recent serverTime for each company. I have used theta style query and hence JOIN is not necessary.