MYSQL: How to display data if foreign key is implode (',') - mysql

I'm stuck on creating MySQL queries. I have two tables, which are user and factory. The data and structure are as below:
table user
enter image description here
table factory
enter image description here
From table user, u can see example, id = 1 got factoryID = 2,3. At table factory, id = 2 is F1 and id = 3 is F2.
Now, how i want to join the table, and display the data like example,
user.id = 1
user.name = Amira
user.factoryID = 2,3
factory.factoryName = F1,F2
Can anyone know how to write the query?

I suggest fixing your table design. Using FIND_IN_SET can do the trick, but you will be facing some performance issues, especially for larger data.
As per the question you could use:
select user.id,name,factoryID,group_concat(factoryName) as factoryName
from user
inner join factory ON FIND_IN_SET(factory.id,user.factoryID)
group by user.id,name,factoryID;
Result:
id name factoryID factoryName
1 Armira 2,3 F1,F2
2 Balqis 4,5 F3,F4
Demo

Related

get rows from database where user is member

I have this table in my MySQL database:
I need a query, but I have a logical problem to "create" them.
I am User with userID "3".
I would like to get all userIDs, which have the same chatToken like me, but without me in the result.
The result should be: userID "1" and "2".
How can I do this?
According to your sample data, userID 3 appears twice and has two different chatTokens. So you want any user that has any one of your chatTokens but is not you (i.e. whose userID is not 3)
select userID
from table
where chatToken in (select chatToken
from table
where userID = 3)
and userId != 3

MySQL query to find values in table with given value and find others that are similar

I have a table called followers and I want to be able to find the current users followers, display that list, and then compare those values against another user ID to see if they are following them or not from that same table
This is the table I'm using currently to get the list of followers for a specific user:
followers
-------
followId - primary key, the unique id for the follow relationship
userId - the user that is following someone
orgId - that someone that the user is following
I tried using a union query but I wouldn't want to due to performance reasons (table may contain high volume of records) and because it isn't scalable (I think)?
The expected output should be the list of orgId's for which the user(the user I am checking against) is following, and another column that shows whether my user(my userId that I provide) is following that orgId value (i.e a following column).
Hmmm, if I understand correctly, you have two users and you want to know which orgs followed by the first are also followed by the second:
select f.orgid,
(exists (select 1
from followers f2
where f2.userId = $seconduserid and
f2.orgid = f.orgid
)
) as seconduserflag
from followers f
where f.userId = $firstuserid

A request to get a Twitter feed like?

How does Twitter (or Facebook) do to retrieve a user feed?
Here is the database schema I am working on. I renamed everything to look like twitter hoping that the most of you would understand my question...
I only have two resources: users and tweets. So here are those two tables:
users:
- id
- username
tweets:
- id
- author_id
- content
Let's continue with a simple pivot table to associate tweets and users:
user_tweet:
- user_id
- tweet_id
- created_at
This pivot table is here mainly to store the retweets. But it also stores the original tweets for more convenience.
For example, let's take user 1 tweets 'something'. user 2 and user 3 retweet. The user_tweet table will have three rows.
And, let's see a last table that complicates everything: The Following System.
Every user can follow an other user. I named the table "followee_follower":
followee_follower:
- followee_id
- follower_id
The followee_id is the user.id of the person being followed
The follower_id is the user.id of the person that follows an other one
Now let's get to the SQL problem:
I'm user 1. I follow user 2, and user 3.
How can I retrieve the tweets and retweets from user 2 and user3 knowing that I want to retrieve them ordering them by the created_at field of the user_tweet table, and that I don't want to get two similar tweets.
Many thanks, any help is highly nice from you,
Have a good day/night.
EDIT: Here are some samples data from tables:
users table
tweets table
user_tweet table
followee_follower table
expected results
I'm not sure I completely understand your question (sample data would help), but I think you just need to use multiple joins:
select t.id, t.content, ut.created_at
from tweets t
inner join user_tweets ut on t.id = ut.tweet_id
inner join followee_follower ff on ut.user_id = ff.follower_id
where ff.followee_id = 1
order by ut.created_at
Perhaps if a user retweets, you'd want to do something like this instead to get the first tweet (assuming the id and created_at fields both should return the minimum):
select t.content, min(t.id), min(ut.created_at)
from tweets t
inner join user_tweets ut on t.id = ut.tweet_id
inner join followee_follower ff on ut.user_id = ff.follower_id
where ff.followee_id = 1
group by t.content
order by min(ut.created_at)

SELECT INTO multiple #variables MySQL

I wish to store the result of a SELECT statment into multiple variables.
I know that the result of this query will always return 6 ints from 6 different rows.
I have tried using the following code :
SELECT id INTO #photo1, #photo2, #photo3, #photo4, #photo5, #photo6
FROM album WHERE uploaded = #time AND scene_id = NEW.id;
album is a table.
uploaded is a field in album.
scene_id is a field in album.
id is the PRIMARY_KEY of album.
I have read that the number of variables must equal the number of fields. Which is obviously not the case in the above statement.
With this in mind, how would I overcome this problem?
This code is being used within a MySQL Trigger.
EDIT : Relevant Table schema as per request :
person -- name of table
id | approved -- id is PK
album -- name of table
id | uploaded | scene_id -- id is PK
Trigger is fired on change of approved from 0 to 1
You can join with the same table and ensure that each join will provide a new id, something like (eg. for two ids, but you will get the point):
SELECT a1.id, a2.id INTO #photo1, #photo2
FROM album a1
inner join album a2 on a2.scene=a1.scene and a2.upload=a1.upload and a2.id>a1.id
WHERE a1.uploaded = #time AND a1.scene_id = NEW.id;
See SqlFiddle for a complete sql and test case.

Need help with mysql query

I am having a text-box which is auto complete.
When i enter the name on the text-box the auto complete will work and provide the list from table.
And when i click the save button the names which are entered in the text-box will inserted in a table. Until this, the process i working fine.
My case is : Already existing users should not show in auto-complete.
Table Structure :
Users Table
uid, name
work table
wid, from_uid, to_uid,
How the mysql query should be.. Any help regarding this will be thankful and gratedful...
select name
from users
where name like 'Jo%'
and uid not in (select to_uid
from work
where from_uid = 666)
Supposing that user entered Jo substring and current user's id is 666
or another query that does the same (not sure which one will be more efficient in your particular case) but uses LEFT JOIN instead of subquery:
select name
from users u
left join work w on w.from_uid = 666
and u.uid = w.to_uid
where name like 'Jo%'
and w.to_uid is null