Users:
UserID | UserName
--------------
1 Jack
2 Evelin
Shop:
ShopID | ShopName | ShopUserID
-------------------------------
1 Prague 1
2 Berlin 2
Is possible get automatically update table:
ShopID | ShopName | ShopUserID
-------------------------------
1 Prague Jack
2 Berlin Evelin
Can I use trigger?
Thanks!
Please use Join please try this query :
Select s.shopID,s.ShopName,u.UserName from Shop as s Left Join Users as u on s.ShopUserID = u.UserID
Output will look like this :
ShopID | ShopName | UserName
-------------------------------
1 Prague Jack
2 Berlin Evelin
Related
i have 2 tables
[series]
--------------
ID | ART
--------------
1 | sculptor
2 | painter
3 | writer
-----------
[artists]
--------------
NAME | ART_IDs
--------------
john | 1
jack | 1,2
jill | 2,1
jeff | 3,1
which I want to join like this:
SELECT se.art, ar.name as artist
FROM series AS se
JOIN artists AS ar ON FIND_IN_SET(se.id , ar.art_ids) > 0
What I get is only the first values:
[result]
-------------------
ART | ARTISTS
-------------------
sculptor | john
sculptor | jack
painter | jill
writer | jeff
Instead of:
[result]
----------------------------
ART | ARTISTS
----------------------------
sculptor | john
sculptor,painter | jack
painter,sculptor | jill
writer,sculptor | jeff
Try this:
SELECT GROUP_CONCAT(se.art ORDER BY FIND_IN_SET(se.id , ar.art_ids)) as art, ar.name as artist
FROM series AS se
JOIN artists AS ar ON FIND_IN_SET(se.id , ar.art_ids) > 0
GROUP BY ar.name
If you want to concat column in each group, GROUP_CONCAT can help you.
Check demo in Rextester.
I have two mysql tables as follows:
contacts
---------------
id | name | email
---------------
1 | Jack | jack#test.com
2 | John | john#test.com
3 | Liz | liz#test.com
5 | Jack | jack#test.com
6 | Liz | liz#test.com
7 | Mike | mike#test.com
8 | Jack | jack#test.com
purchases
-------------------
id | contact_id | paid
-------------------
1 | 3 | true
2 | 5 | true
I need unique contact_ids that made purchase and other unique contact_ids that don't have made purchases.
So the final result will be as:
-------------------
id | name | email
------------------
2 | John | john#test.com
3 | Liz | liz#test.com
5 | Jack | jack#test.com
7 | Mike | mike#test.com
I tried the query as:
SELECT * FROM contacts LEFT JOIN purchases ON contacts.id = purchases.user_id
But this is not giving me unique rows as required. I tried several combination of DISTINCT, but I am not getting the result as required.
did you try this?
SELECT COALESCE(purchases.contact_id, contacts.id) as id, name, email
FROM contacts
LEFT JOIN purchases ON contacts.id = purchases.user_id
GROUP BY name
SQL FIDDLE
Something like that should work, but its performance is "?".
SELECT * FROM contacts WHERE id IN (
SELECT DISTINCT id as i FROM purchases
UNION
SELECT DISTINCT contact_id as i FROM purchases
)
GROUP BY id
I have a MySQL table called employee that looks like this:
ID | User | Phone_No | Phone_No_Count
1 | Fred | 9999 | 1
2 | John | 8888 | 2
3 | Pablo | 123 | 1
4 | John | | 0
5 | John | 8888 | 2
6 | Pablo | | 0
7 | John | 456 | 1
Phone_No_Count is a count of the Phone_No column, if there is no Phone_No then Phone_No_Count is set to zero.
I want to backfill the missing Phone_No entries using Phone_No entries which have the highest Phone_No_Count.
e.g. User John has 2 Phone_No's (8888 and 456) so I just want to use 8888 as it has the highest Phone_No_Count (2)
The backfilled data in employee would then look like this:
ID | User | Phone_No | Phone_No_Count
1 | Fred | 9999 | 1
2 | John | 8888 | 2
3 | Pablo | 123 | 1
4 | John | 8888 | 0
5 | John | 8888 | 2
6 | Pablo | 123 | 0
7 | John | 456 | 1
I can then update the Phone_No_Count separately, which I know how to do anyway.
All the examples I've seen online are for backfilling multiple tables or if it's just one table they don't have the required logic for this.
Can somebody please help as this has been frying my brain all day!!
One way to go about this kind of update you can use user defined variables in your query and store the phone for the user which has the maximum of phone count (i.e a correlated subquery) then join this data with your table and do update
update Table1 t1a
inner join(
select t1.id,
t1.`User`,
#p:= case
when t1.Phone_No is null then #c
else t1.Phone_No END Phone_No,
#c:=(select Phone_No from Table1 where t1.`User`=`User` order by `Phone_No_Count` DESC limit 1 ) max_phone
from Table1 t1,(select #p:=0,#c:=0) t
order by t1.`User`,t1.`Phone_No_Count` DESC
) t2 on(t1a.id=t2.id)
set t1a.Phone_No = t2.Phone_No
Fiddle Demo
The trick is to get the phone number for the highest count. Unfortunately, MySQL doesn't let you have subqueries on the same query being updated, but you can do this with a trick. This allows you to use update/join syntax:
update employee e join
(select e.user,
substring_index(group_concat(phone_no order by phone_no_count desc
), ',', 1) as new_phone_no
from employee e
group by e.user
) toupdate
on e.user = toupdate.user
set e.phone_no = toupdate.new_phone_no
where e.phone_no is null;
I have two table like following ones:
users
-----
id | name
_________
1 | mert
2 | ersin
3 | faruk
friends
-------
id | follower | following
_________________________
1 | 2 | 3 |
2 | 3 | 1 |
__________________________
I want to find people who don't follow the selected person.
For example:
get_people_not_follow(2) // "ersin"
Result: array(array(1, 'mert'), array(3, 'faruk'));
get_people_not_follow(1) // "mert"
Result: array(array(2, 'ersin'));
Thanks for your help.
SELECT users.id, name
FROM users LEFT JOIN friends ON (following = users.id AND follower = ##)
WHERE friends.id IS NULL
Where ## is the ID you are interested in.
I have UserComments table like this :
1 | Frank | hello world
2 | Jane | Hi there
3 | Frank | this is my comments
4 | Frank | I think I need some sleep
5 | Jason | I need to buy new MacBook
6 | Jane | Please invite my new Blackberry PIN
On the other hand, I have FriendList table contains :
1 | Jason
2 | Jane
Let's say my friends ID always BETWEEN 1 AND 5.
And since Frank is not my friend, I'm not able to see his comments. how to have combined tables like this (ORDER BY UserComments.ID DESC) :
1 | Jane | Please invite my new Blackberry PIN
2 | Jason | I need to buy new MacBook
3 | Jane | Hi there
thanks.
Try this:
SELECT A.ID, B.UserName, B.Comment
FROM FriendList A
INNER JOIN UserComments B ON A.ID = B.ID
ORDER BY A.ID DESC
Try this::
Select
*
from
user_comments inner join friend_List on (join criteria)
where user_Id = ? order by user_comments.id desc