I would like to ask for ways to solve this MySQL issue.
Currently, I have this select query
SELECT username, friend_username from friends
WHERE (username = "apple" or friend_username = "apple");
and it shows this Friends Table (below).
Friends Table
------------------------------
| username | friend_username |
------------------------------
| apple | orange |
| apple | pear |
| durian | apple |
------------------------------
But I would like to execute a select query to make it look like this (below).
------------
| username |
------------
| orange |
| pear |
| durian |
------------
Is there any possible ways to achieve this? Appreciate the help! Thank you.
I would write this as:
select case when username = 'apple' then friend_username else username end username
from friends
where 'apple' in (username, friend_username);
The where clause uses in to filter on rows where either username or friend_username is equal to 'apple'. Then, the case expression in the select clause displays the "other" column.
Related
I'm working on a legacy system that allowed the insertion of multiple entries with the same email. In the people table are present entries with same name and email and also different name with an already used email (es. the user didn't know or didn't ask the email address to the person and chose to put a fake one).
A person could subscribe to a User multiple times yearly basis
They asked me for a report of which users entered the most of these entries.
Let's say I have 3 tables
| people| | subscriptions| |users|
| ------| |--------------| |-----|
| id | | id | |id |
| name | | personId | |name |
| email | | userId |
| subYear |
I found all duplicate emails and their occurrences using this query
SELECT users.name, people.email, count(subscriptions.id) nSub
FROM people
INNER JOIN (SELECT email, count(id) occurrences
FROM people
where email is not null and email != ""
GROUP BY email
HAVING occurrences > 1) duplicates
ON people.email = duplicates.email
JOIN subscriptions ON people.id = subscriptions.personId
JOIN users on users.id = subscriptions.userId
group by users.name, people.email;
but now I'm stuck when I have to integrate users, the query gives incorrect results or gets stuck in a loop.
I'm sure I'm getting the grouping wrong but I got lost
The result I'm trying to achieve is something like (based on data provided in fiddle)
|users.name| people.email | occurrences |
|----------|-------------------------|-------------|
| User1 | example#example.com | 1 |
| User2 | example#example.com | 2 |
| User2 | fake#email.com | 3 |
| User3 | fake#email.com | 1 |
Any suggestion you can give me is welcome. Thank's in advance
UPDATE: Sorry for the sloppiness, I created a fiddle
sql-fiddle
I have a table called users:
+----+---------+--------+----------------+----------------+----------+
| ID | Name | Zip | Email | Phone | Username |
+----+---------+--------+----------------+----------------+----------+
| 0 | Jill | 33333 | jill#aol.com | (123)123-1245 | idjill |
| 1 | Jack | 11111 | jack#aol.com | (123)111-1111 | idjack |
| 2 | Bob | 66666 | bob#aol.com | (123)222-2222 | idbob |
| 3 | jMarie | 12345 | jill#aol.com | (123)123-1245 | none |
+----+---------+--------+----------------+----------------+----------+
If I run SELECT * FROM users WHERE Phone=(123)123-1245 will return both ID# 0 and 3.
What I would like to do is be able to select the user and but also return any other users that have the same phone or email but not zip code. So for example if I run SELECT * FROM users WHERE Username= idjill I'd like it to return user 0 and 3 because they both have the same phone number.
How can I do that? Thanks. If anyone has a better idea for a title to this post, please share. My first post, sorry.
Edit: I think I need to clarify my question a bit. So I have this select query right here:
SELECT * FROM users WHERE Username = 'idjill' OR Email = 'idjill'
That perfectly returns ID 0, I would like it to return ID 0 and 3. Because the phone and the email match (I am using the same input to search between username and email).
How can I expand on this?
Using INNER JOIN like below.
SELECT DISTINCT a.* FROM users a INNER JOIN
(SELECT * FROM users WHERE Username='idjill') b
ON (a.Phone=b.Phone OR a.Email=b.Email) AND a.Zip<>b.Zip;
Nested query can be used
Select *
from users
where Phone = (select Phone from users where Username = "idjill");
You can use nested query like this.
SELECT *
FROM users
WHERE Phone=(SELECT PHONE
FROM users
WHERE Name='Jill') OR
Email=(SELECT Email
FROM users
WHERE Name='Jill');
I got table called 'pet' have the following values
id | name | nickname | dateofborn |
1 | test | jhon | 2009 |
2 | test2| test | 2010 |
3 | mike | NULL | 2010 |
3 | jhon | testor | 2011 |
I want to select all of columns that contain 'test' value either in name column or nickname column so i have this query didn't actually work for me:
SELECT * FROM pet WHERE name='test' OR nickname= 'test'
note that I exactly want test value not testor.
The query is right, I think you have some problems with your Table.
But try like below:
USE [yourDB es: animals];
SELECT * FROM [yourDB].pet WHERE (name='test' OR nickname='test');
try this.
SELECT * FROM pet WHERE name like 'test%' OR nickname like 'test'
I have a table in mysql in this structure
table: member
| Id | Name | Lastname | Username
| --------------------------------
| 1 | Alexi| Lalas | alexi
| 2 | Jack | Louis | louis
And I have a table called member images with this structre:
table: image
| Id | MemberId | Image | Type |
|------------------------------------|
| 50 | 1 | face.jpg |Avetar |
| 51 | 1 | image.jpg |Gallery|
| 52 | 2 | main.jpg |Avetar |
| 53 | 2 | jungle.jpg |Gallery|
And I want to get this result
| Id | Name | Lastname | Username | Image1 | Image2 |
|-------------------------------------------------------|
| 1 | Alexi| Lalas | alexi |face.jpg |image.jpg |
| 2 | Jack | Louis | louis |main.jpg |jungle.jpg|
Becuase of some reasons I can't handle is on app side and I have to do it on sql side.
Imagin that I always have 2 type of images and we always have Image1 and Image2.
Any help would be appritiated.
Since they are only two types, you can use the CASE expression to do so. Something like this:
SELECT
m.Id,
m.Name,
m.LastName,
m.UserName,
MAX(CASE WHEN i.Type = 'Avetar' THEN i.Image END) AS 'Image1',
MAX(CASE WHEN i.Type ='Gallery' THEN i.Image END) AS 'Image2'
FROM member AS m
LEFT JOIN image AS i ON m.Id = i.MemberId
GROUP BY m.Id,
m.Name,
m.LastName,
m.UserName;
Note that: LEFT JOIN will include all the members from the member table even if they have no images in the image table, in this case NULL will be returned.
See it in action here:
SQL Fiddle Demo
The way to do this is via a subselect (subquery). You would subselect the first column as the image for the user withe Avetar type and the other column subselect would be for the image with the type Gallery. If you can't find a way to make User ID and Type unique in your sub table then you are going to have issues with this type of query. Keep in mind that a subselects can impact performance heavily.
http://dev.mysql.com/doc/refman/5.0/en/subqueries.html
I have a table that looks something like this:
+-------------+----------+---------------+------------+
| Contract_ID | Name | Username | Password |
+-------------+----------+---------------+------------+
| 12345671 | Facebook | john.doe | password |
| 12345672 | Google | john.doe | password |
| 12345673 | Apple | martha.malone | doodlebear |
| 12345674 | Samsung | jimmy47 | parkour445 |
| 12345675 | HTC | rick.grimes | simpsons33 |
+-------------+----------+---------------+------------+
I'd like to select only records where there is a one to one ratio between username/password combination and contract id. In this case, that would mean I'd like to select the 3rd, 4th and 5th records in this table.
I've tried different combinations of the DISTINCT keyword but that doesn't seem to be the correct route to go. Is there a query that can return this information?
As a sort of bonus question: is there a query that can produce the opposite results (ie. only records where there is greater than 1 to 1 ratio between contract ids and username/password combination)?
You should use the GROUP BY clause together with the HAVING clause, for example:
SELECT Username, Password FROM Table
GROUP BY Username, Password
HAVING COUNT(*) = 1
The opposite is:
SELECT Username, Password FROM Table
GROUP BY Username, Password
HAVING COUNT(*) > 1
Group by (Name, Password) doesn't work?
Very naughty way of doing it,not good at all, but it works:
select distinct
least(col1, col2) as value1
, greatest(col1, col2) as value2
from yourtable