mySQL get row twice if field = this - mysql

I'm pulling a list of names from a mySQL table called users with the layout something like this;
id name anon
--------------------------------
1 Roger 0
2 Craig 1
3 LOL 0
For users where anon=1 I want to pull them twice, so the records I'd get from the above table would be 4 instead of 3:
1) Roger
2) Craig
3) Craig
4) LOL
I can imagine there's an easy way to do this but I'm getting brain block. Any suggestions?

Do a
Select name from users
union all
select name from users where anon = 1
and order them by name if desired?

Related

Mysql stored procedure Pass Guid list to parameter and use for in clause [duplicate]

I have a table like:
id name children
1 Roberto Michael,Dia
2 Maria John,Alex
3 Mary Alexandre,Diana
My problem is;
I want to find who has a child named Alex.
I can't use "where children = 'Alex'" in SQL because I have more than one names in same cells.
So I use "where children LIKE '%Alex%'" - that looks smart but
in the same time i get all start like Alex :( Alexandre
or i want to get dia but result is dia and diana :(
how can I get single Alex in that data type?
I hope I can explain my problem with my terrible english :D
The best solution would be to normalize your schema. You should have a separate table with one row for each child, instead of a comma-delimited list. Then you can join with this table to find parent with a specific child. See #themite's answer for an example of this.
But if you can't do that for some reason, you can use FIND_IN_SET:
WHERE FIND_IN_SET('Alex', children)
You should split the data into two tables.
the first would look like this
ID Name
1 Roberto
2 Maria
3 Mary
And the second like this
ParentId child
1 Michael
1 Dia
2 John
2 Alex
and so on.
then you could do the query you want without having to worry about like and your data is much more useable
That's why you'd want to have two tables here.
parents:
id name
1 Roberto
2 Maria
3 Mary
children:
id parentid name
1 1 Michael
2 1 Dia
3 2 John
4 2 Alex
5 3 Alexandre
6 3 Diana
And now you can query this much more effectively with a join or an exists:
SELECT *
FROM Parents
WHERE EXISTS(
SELECT *
FROM Children
WHERE parentid=Parents.id
AND Children.name='Alex'
)
I would rather make different tables for children and parents something like this.
Table for parents
parent_id name
1 Roberto
2 Maria
3 Mary
Table for children
children_id parent_id name
1 1 Michael
2 1 Dia
3 2 John
.... and so on

Define table of choosed user row in multiple table list

I have control dashboard where multiple tables are listed with query
And in dashboard I can switch it to one table from ALLData to User1Table... and vice versa.
When there is only one table chosed I can easily manipulate data. However, I am struggling with updating rows when ALLData(all tables) are listed in dashboard. I can update it checking each table. I was wondering is there any better way to update it.
Tables have no DR. All tables have same column names.
//ALLData
SELECT * FROM users1
UNION ALL
SELECT * FROM users2...
user1
id name tel status
1 Bob 911 1
user2
id name tel status
3 Anna 11 0
3 Jack 12 1
//ALLData in dashboard
id name tel status
1 Bob 911 1
3 Anna 11 0
3 Jack 12 1
I can use id and status as PK

Get unique data across certain column in multiple tables - sql

I know this is probably so odd to ask. But lets say I have 3 tables:
Table 1
ID
Name
1
Adam
2
David
3
Conor
Table 2
ID
Name
1
Adam
2
Derek
3
Niall
Table 3
ID
Name
1
Adam
2
David
3
John
Is there any way I can write a query to get the unique names across all 3 tables. So it would return "Adam, David, Conor, Derek, Niall, John"
Order doesn't matter
If it helps, all name values are related to a names table
yes , one way is to union them
select name from table1
union
select name from table2
union
select name from table3
union automatically removes duplicate cases

MySQL table comparison between two tables

I have two tables, follow and followed. I want to get all the rows in the follow table such that follow.screen != followed.following_screen_name.
follow table
ID screen_name
-----------------
1 eddie
2 jason
3 omar
4 jonathan
5 jack
followed table
ID my_screen_name following_screen_name
-------------------------------------------
1 john eddie
2 kenny eddie
3 kenny omar
4 john jason
5 john omar
Query I tried which didn't work
SELECT follow.screen_name from follow, followed where followed.my_screen_name='john'
AND follow.screen_name != followed.following_screen_name
Expected results
ID screen_name
-----------------
1 jonathan
2 jack
you can get this by doing a LEFT JOIN
SELECT F.screen_name FROM follow F
LEFT JOIN followed FD
on F.screen_name = FD.my_screen_name
OR F.screen_name = FD.following_screen_name
WHERE FD.my_screen_name IS NULL
and FD.following_screen_name IS NULL
Another way is to use NOT EXISTS, get all rows that exists in followed and do NOT EXISTS clause to get desired result.
SELECT F.screen_name FROM follow F
WHERE NOT EXISTS
(
SELECT 1 FROM followed FD
WHERE F.screen_name = FD.my_screen_name
OR F.screen_name = FD.following_screen_name
)
There are plenty of ways to solve this, but common to all is that you need to compare the follow.screen_name to both followed.my_screen_name and followed.following_screen__name.
One way is to use NOT IN with a UNION:
select screen_name
from follow
where screen_name not in (
select following_screen_name
from followed
where following_screen_name is not null
union all
select my_screen_name
from followed
where my_screen_name is not null
)
While this approach is nice for clarity, it may not be as good for performance as using a left join or not exists.
A nice place to pick up mysql syntax and logic is here.
But try this code, it selects every row where the screen_name is not identical to anything produced in the next two queries:
SELECT * from follow WHERE screen_name
not in (select screen_name from followed)
AND not in (select followed_screen_name from followed);
The last two queries would look this and the WHERE filters all of the rows out with screen names identical to the fields below.
my_screen_name following_screen_name
-------------------------------------
john eddie
kenny eddie
kenny omar
john jason
john omar

Listing all rows that have a duplicate entry in a particular column in MySQL table

Despite a good deal of searching I haven't come up with the correct method of listing all columns for rows that have the same content in a particular column (let's say the column 'Name').
So if my table, called USERS, had the following content:
ID User Name
1 Nick Nick
2 NickP Nick
3 NickC Nick
4 John John
5 Brian Brian
The SELECT statement should return:
ID User Name
1 Nick Nick
2 NickP Nick
3 NickC Nick
As these are all the rows that contain the same content in column 'Name'. How would I write this?
How about this?
SELECT * FROM Users WHERE Name IN(SELECT Name FROM Users GROUP BY Name HAVING COUNT(1) > 1)