Users:
userid username password
1 venkat reddy sa
2 reddy as
3 re sa
Location:
id userid lat lon
1 1 14.00 15.00
2 2 15.00 17.23
3 3 15.00 17.23
Friends:
id userid friendid isActive
1 1 2 true
1 2 1 true
I need to query like if I pass userid=1 and username=re, then I need to get all rows starting with re and if that user is already friend to that userid give status message like already friend and his username and userid. If a user is not a friend as of now then give his details like username and userid.
Please tell me ....
Ex: userid =1 username=re
then I need output like
<OutputResponse>
<Response>
<userid>2</userid>
<username>reddy</username>
<lat>15.00</lat>
<lon>17.23</lon>
<status>already friend</status>
</Response>
<Response>
<userid>3</userid>
<username>re</username>
<lat>15.00</lat>
<lon>17.23</lon>
<status>not a friend as of now</status>
</Response>
</OutputResponse>
you can start with something like this...
int userid=1;
string username="re";
var result=(from q in Users
join qq in Location on q.id equals qq.userid
where q.Username.StartsWith(username)
select new
{
userid=q.Id,
username=q.Username,
lat=l.lat,
lon=l.lon,
status=Friends.Where(z=>z.Friendid==q.Id&&z.Userid==userid).Count()>0?"already friend":"not a friend as of now"
});
Related
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
I have recently taken over a email campaign project and need to generate a report for the customer. However the data has been stored very strangely.
Basically the client wants a report of the subscribers first name and last name that have subscribed to a emailing list.
Example table data.
------------------------------------------------------------
id | owner_id | list_id | field_id | email_address | value
------------------------------------------------------------
1 10 1 137 me#example.com John
2 10 1 138 me#example.com Doe
So as you can see, John Doe has subscribed to mailing list 1, and field_id 137 is his first name and field_id 138 is his last name.
The client is looking for a export with the users first name and last name all is one field.
I tred the following sql query
SELECT value
FROM Table_A AS child
INNER JOIN Table_A AS parent
ON parent.email_address = child.email_address
WHERE child.owner_id = '10'
But unfortunately the query gives me the results in many rows but not appending the first name and last name into one field,
If anyone can provide some assistance that would be awesome.
Thanks.
SELECT
concat( parent.value,' ',child.value)name
FROM mytable AS child
left JOIN mytable AS parent
ON parent.email_address = child.email_address
WHERE child.owner_id = '10'
and parent.field_id=137 and child.field_id=138
Check at-http://sqlfiddle.com/#!9/199b4b/45
I think you have to use a variable to put in there everything you have to and then select the variable with the desired name of yours.
For example:
DECLARE #yourvariable VARCHAR(MAX)
SELECT #yourvariable = COALESCE(#yourvariable + " ") + value
FROM table_A
WHERE owner_id = 10
SELECT #yourvariable as FullName
Try that, it might help.
You can try this code(column name equals value in your original DB):
select a.name
from
table_a a inner join table_a b
on a.email_address = b.email_address and a.field_id <> b.field_id
where a.owner_id=10
order by a.field_id
Here is the example link:
http://sqlfiddle.com/#!9/5fbdf6/25/0
As per assumptions, first name has the field id 137 and last name has the field id 138.
You can try the following query to get the desired result.
SELECT CONCAT(SUBSTRING_INDEX(GROUP_CONCAT(`value`),",",1)," ",SUBSTRING_INDEX(GROUP_CONCAT(`value`),",",-1)) AS client_name
FROM Table_A
WHERE owner_id = 10
AND field_id IN (137, 138)
GROUP BY email_address;
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
My website has a comment system, and the comments are stored in a table that looks like the following:
post_id | path | total_replies
1 1/ 3
2 1/2/ 2
3 1/2/3/ 1
4 1/2/3/4/ 0
In words, post id 2 is a reply to post id 1, post id 3 is a reply to post id 2 which is a reply to post id 1, etc. It's literally just a path.
The total_replies column for post id 1 is 3 because there are 3 replies made to it (post id 2, 3, and 4), and the same logic is used for the other rows.
Because this is a comment system, more rows are added each time a new comment is made. What I'm trying to do is find a way to increment the total_replies column each time a new column is made.
So for example, if someone made a new reply (with post id 5) to post id 2, the table would look something like this:
post_id | path | total_replies
1 1/ 4 (this was incremented)
2 1/2/ 3 (this was incremented)
3 1/2/3/ 1
4 1/2/3/4/ 0
5 1/2/5/ 0
So, my question is, how would I do the incrementing each time a new reply is made?
EDIT: My current query.
$stmt = $cxn->prepare("UPDATE posts SET total_replies = total_replies+1 WHERE ? LIKE concat(?, '%') AND post_path != ?");
$stmt->bind_param('sss', $new_path, $path, $new_path);
$stmt->execute();
You can do what you want with a update and where clause:
update comments
set total_replies = total_replies + 1
where NEWPATH like concat(path, '%');
In this case, NEWPATH would be '1/2/5/'. It is unclear whether you have this in a variable or only as a new row in the table. This assumes that you have it as a variable.
EDIT:
If you do not want to update the original record:
update comments
set total_replies = total_replies + 1
where NEWPATH like concat(path, '%') and NEWPATH <> path;
I want to fetch records from first table & check that records are exist in second table or not:
tbl_user
userid email
-------------------------
1 abc#gmail.com
2 abcd#gmail.com
3 abedd#yahoo.com
4 xyz#gmail.com
5 test#ymail.com
tbl_user_responce
id responce_email
-------------------------
1 abc#gmail.com
2 abcd#gmail.com
3 abc#yahoo.com
4 xyz#gmail.com
5 abcd#ymail.com
UPDATE
Note: In my secord table email is stored in xml format like following:
<?xml version="1.0" encoding="UTF-8"?>
<user>
<userinfo>
<email>stevemartin148#gmail.com</email>
<status>1</status>
<description>Success</description>
</userinfo>
</user>
i want to fetch those records that are not exist in tbl_user_responce from tbl_user
So from above records i want result like
Email
-----
abedd#yahoo.com
test#ymail.com
Does anybody know how to manage that?
Thanks in advance.
How about something like
SELECT *
FROM tbl_user
WHERE NOT EXISTS (
SELECT 1
FROM tbl_user_responce
WHEREN tbl_user.email = tbl_user_responce.responce_email
)
Or even something like
SELECT tbl_user.*
FROM tbl_user LEFT JOIN
tbl_user_responce ON tbl_user.email = tbl_user_responce.responce_email
WHERE tbl_user_responce.responce_email IS NULL
The query you're asking for won't use any indexes, but this would seem to do the job as long as there's only one email per "responce";
SELECT email
FROM tbl_user
WHERE email NOT IN (
SELECT SUBSTRING_INDEX(
SUBSTRING_INDEX(responce_email,'<email>', -1),
'</email>',1)
FROM tbl_user_responce;
)
From the "I'd do an SQLfiddle if it weren't down" dept.