MYSql multiple join query kando - mysql

I have a question for school that I can't quite figure out.
For each card on the board with the name “Product launch”, list the details of the card, the title of the column it is on, who created the card and member(s) that the card is assigned to.
I'm trying to work through this put not getting anywhere really.
select card.description, (select user.UserName where card.Creator = user.userID)
FROM card, user, board, `column`
JOIN board ON board.BoardID=`column`.boardID
where board.boardname = "Product launch"
Any help would be appreciated

10/10 for honesty in admitting that you are asking the internet to do your homework for you ;-) You will find that you are more likely to get help if you show clear signs of trying to help yourself.
If you are struggling then start by taking small logical steps and building it up progressively. Start with just retrieving the board with the given name (yes, I know you have already done this) -
SELECT *
FROM `Board`
WHERE `Board`.`BoardName` = 'Product launch'
then the next step is to satisfy the "for each card on the board" requirement. Based on your ERD we know we need to get the Columns to get to the Cards, so add Columns next -
SELECT `Board`.`BoardName`, `Column`.`ColumnTitle`
FROM `Board`
INNER JOIN `Column`
ON `Board`.`BoardID` = `Column`.`BoardID`
WHERE `Board`.`BoardName` = 'Product launch'
then the Cards -
SELECT `Board`.`BoardName`, `Column`.`ColumnTitle`, `Card`.*
FROM `Board`
INNER JOIN `Column`
ON `Board`.`BoardID` = `Column`.`BoardID`
INNER JOIN `Card`
ON `Column`.`ColumnID` = `Card`.`ColumnID`
WHERE `Board`.`BoardName` = 'Product launch'
then continue the process to add the Creator and then the member(s) via CardAssignment. You may want to look at GROUP BY and GROUP_CONCAT when adding the member(s).
If you continue to struggle don't just say I cannot figure it out. Have a go and then come back with details of what you have tried and what hasn't worked as you expected. Researching and having a go are huge parts of the learning experience.

Related

I can't find a way to implement messages in my school database on MySQL

I'm working on a school database on which I would like to implement messages that will be created by the schools for the parents to view.
The workflow goes like this:
1. The school sends a message to a certain group of students, it could be a message to all the students from that school, or a message to just the first year, or a message to classroom 1B (1 being the year and B the group), or even a message to just 1 student.
2. Parents access a platform on which they will see the messages regarding their children.
For example:
if the school sends a message to the classroom 1B, only parents with children on that classroom will be able to see it.
if the school sends a message to the first year, only parents with
children on the first year will see it.
What I need help with is:
How could I arrange the database in order to accomplish the message
filtering (By school, by year, by classroom (1B, 2A,
etc.) and by student)?
What would be the sentence that I need to use in order to retrieve
the messages for a parent regarding their children?
I hope I explained myself well, please feel free to ask any question you have, and thank you so much :)
Here's a pic of the database:
If I understood well, for this question "What would be the sentence that I need to use in order to retrieve the messages for a parent regarding their children?" you could use a simple inner join between message and parent_detail on id_students are equals where id_parent is your parentID:
SELECT * FROM `message` m
INNER JOIN `parent_detail` p_d on p_d.ID_student = m.ID_student
WHERE p_d.ID_detail = 'parent_id_variable'
Regarding the first question, using the same principle, you need to use an inner join between message students and schools (if you want the name of the school and not only the ID) and apply in where condition what parameter you want.
For example, by school => message.ID_school, by school and by year => message.ID_school and students.year.

SQL Is this Outer or Inner Join and how to join on Array

Seemed simple when I started and have done this before, now I confused myself and at a road block.
Have two tables: News_Table and a People_Table. Under the News_Table there is a field: News_People_Contributed and it has the ID's of the People_Table in array format (1,4,7,10) thus Four People contributed. I am creating a search parameter that looks up News_Header AND News_People_Contributed and can't figure how to create the search column.
News_Table
News_ID
News_Header
News_People_Contributed
People_Table
People_ID
People_First_Name...
Is it something like...
Select*
From News_Table
Left Join News_Table
On People_Table.People_ID IN (News_Table.News_People_Contributed)
Where Search_Param Like '%News_Header%' OR Search_Param Like '%People_First_Name%'
The problem is (News_Table.News_People_Contributed) is a string and the ID's are not. Plus I may not have people contributed etc. To make the issue even more complex, I'm doing this in MS Access instead of MySql, so have to code it "old school" sql for work around.
Perform a cross join and filter on matches in the string list. It says nothing about efficiency or form (as already commented on), but it works.
SELECT *
FROM News_Table, People_Table
WHERE InStr([News_People_Contributed],CStr([People_ID])) > 0;
This only answers part of the problem: The join -- the issue everyone seemed concerned about in the initial comments. There are not enough details about about the Search_Parameter to provide help on that. Supply more detail if you need more help there.

Append string to another table in MySQL

I’ve got a problem selecting some things in my database.
I’ve got 1 database with a lot of tables, however I only use 2 with this specific task.
I have a Clothing table, and I need to import this in my new database. I have succesfully transferred a lot of data, but now I need to take 1 final small step into completing this. In my Clothing table I have a column called Youtube link, here I have a link with an Youtube link, this specific link, in this specific column, I want to append that to another table I have in the database, let’s call it new_clothing_table. I there have a description column called prod_desc and I want to append the Youtube link to that column.
But there is also another “problem”, it’s not that every product has a Youtube link, so things have to be filtered in order to not fuck things royally up. A advantage I have, I have in both tables a product_name, and these are all the same. So I want to transfer that specific Youtube link if it’s there (sometimes there is a 0 filled in or nothing, but I dont think it’s NULL because if I make a SELECT query where Youtube_link is null I get zero rows..)
So can someone help me out>?
mysql has an update-join construct you can use:
UPDATE new_clothing_table nct
JOIN clothing c ON c.product_name = nct.product_name AND
c.youtube_link != '0'
SET nct.description = CONCAT(nct.description, c.youtube_link)
EDIT:
To make sure this does what you want, you could first select the updated content in order to examine it:
SELECT nct.description AS old_description,
CONCAT(nct.description, c.youtube_link) AS new_description
FROM new_clothing_table nct
JOIN clothing c ON c.product_name = nct.product_name AND
c.youtube_link != '0'

inner query of subqery returning multiple rows

I am not that experience in sql so please forgive if its not a good question to ask,but i researched around almost for 3-4 days but no able to solve.
My problem is i have a table which have multiple image names in it,so what i have to do is whoever is the follower of a particular user i have to get the imaged from this table,so one user there can be multiple followers,so i have to fetch the images posted by all the followers.
Here is the subquery code snippet i am using.
SELECT id,
outfit_image,
img_title,
description
FROM outfitpic_list r2
WHERE Email=ANY(SELECT being_followed
FROM follower_table
WHERE follower='test#gmail.com')
So the inner query here returns multiple values,for each value(being_followed) i have to fetch all the images and display it,but with this query each time i get only one image.I tried IN also but didnot work out.
Table structure:-
Outfitpic_list table
id|outfit_image|datetime|Email|image_title|description
Follower_table
bring_followed|follower
Please help,I am stuck..!!
Thank you..!!
I think your problem may be the = sign between "E-mail" and "Any". Try this statement:
SELECT
id,
outfit_image,
img_title,
description
FROM outfitpic_list r2
WHERE Email IN
(
SELECT being_followed
FROM follower_table
WHERE follower='test#gmail.com'
)
It's the same statement, without the = sign, and the ANY keyword replaced with IN. (I cleaned it up a little to make it more readable)

database structure for google plus circles?

I know for sure that Google does not use mysql, but in my case I happen to work on a project using mysql and has features that are very similar to circles:
user can belong to many circles
user can be add/removed from circles
posts can be public or can be shared to circles/individual users
if a post is shared to a circle, and new user is added to this circle then this user can also view the post.
If a post is shared to a circle, and an user is removed from this circle then: a. he/she can still view the post if he/she replied in the post b. he/she cannot view the post anymore otherwise
As you can already see, with the above requirements there are a lot going on in the database. If I really share both to circles and individual users, i will probably need 2 One2Many tables. If I share only to individual users by getting the list of users for each circle at the very beginning, then I run into troubles later on when users edit these circles.
Currently, my get-around hack is to share to circles only, even for each individual user I create a 1 user only circle.
So my current database tables look a bit like this:
circle_to_user:
id
circle_id
user_id
friend_id
post:
id
user_id
is_public
post_to_circle
id
post_id
circle_id
To query out the list of posts a user can view, the query is rather complicated and consists of multiple joins:
$q = Doctrine_Query::create()
->addSelect('s.*')
->addSelect('u.id, u.first_name, u.last_name, u.username, u.avatar')
->from('UserStatus s')
->leftJoin('s.User u')
->orderBy('s.created_at DESC');
$userId = sfContext::getInstance()->getUser()->getUserId();
if ($userId == $viewUserId) {
$q->orWhere('s.user_id = ?', $userId);
$q->orWhere('s.user_id IN (SELECT DISTINCT cu1.friend_id FROM CircleUser cu1 WHERE cu1.user_id = ?) AND s.is_public = ?', array($userId, true));
$q->orWhere('s.id IN (SELECT DISTINCT(us2.id)
FROM
UserStatus us2 INNER JOIN us2.UserStatusCircles usc2 ON usc2.user_status_id = us2.id
INNER JOIN usc2.Circle c2 ON c2.id = usc2.circle_id
INNER JOIN c2.CircleUsers cu2 ON cu2.circle_id = c2.id AND cu2.friend_id = ?)', $userId);
} else {
$q->orWhere('s.user_id = ? AND s.is_public = ?', array($viewUserId, true));
$q->orWhere('s.id IN (SELECT DISTINCT(us1.id)
FROM
UserStatus us1 INNER JOIN us1.UserStatusCircles usc1 ON usc1.user_status_id = us1.id AND us1.user_id = ?
INNER JOIN usc1.Circle c1 ON c1.id = usc1.circle_id
INNER JOIN c1.CircleUsers cu1 ON cu1.circle_id = c1.id AND cu1.friend_id = ?)', array($viewUserId, $userId));
$q->orWhere('s.id IN (SELECT DISTINCT(us2.id)
FROM
UserStatus us2 INNER JOIN us2.UserStatusCircles usc2 ON usc2.user_status_id = us2.id AND us2.user_id = ?
INNER JOIN usc2.Circle c2 ON c2.id = usc2.circle_id
INNER JOIN c2.CircleUsers cu2 ON cu2.circle_id = c2.id AND cu2.friend_id = ?)', array($userId, $viewUserId));
}
I hope that the above info is not too long, I just want to give lots of details. My questions are:
Given the above requirements, is my implementation good enough, or is there anything I should change to make it better?
I want to search for articles regarding this type of specific database design problem but could not find much, is there any technical term for this type of database design
Would you suggest any alternatives such as using another type of database, or perhaps index the posts with a searchengine like elastic and let it handle the search instead of using mysql?
Thank you very much for reading until this point, if you find anything I should change in the question to make it easier to follow and to answer, please do let me know.
while your single user circle sounds like a nice try, how will you go about distinguishing it on the way out? when you see a post is shared to a circle, how do you know if that circle is a genuine one or a single user? because i imagine you want to display them differently on the interface. and you'd probably need to know when you fetch the fake circles from the db that the user should not be able to edit them.
while you might get away with avoiding linking to users you now have to handle special circle cases. i'd say go with your 2 x 1-* tables that link a post to multiple circles and separately to multiple users.
perhaps to encourage you to review your intention and leaving aside the 'friend' relationship that may add a special case, as i see it you're more or less looking to: fetch all posts that are public, or are shared with my user, or are shared with a circle i am in, or are posts that i replied to. that isn't too complicated i don't think and you don't have to get a list at the beginning or anything.
(on a related note, multiple JOINs is not a problem that stands out. more importantly you have multiple sub-queries. usually that is bad news. in most cases they can be reworked as normal joins and usually more cleanly).
This kind of problem is mostly not solved with a relational model , i think google uses the datastore , which then sits on bigtable , cassandra and hadoop equivalent.
I am also in same problem but i can suggest you something that i allready covered/completed that dont make two tables for post instead of that add column in Posts table named with/circle_id.
And i also i want to tell you that add a/or more default circle entry(specifically Public and also All Friends/Circles in Circles table.
Now your Post Pickup query will be like this.
$id=$_SESSION["loged_in_user_id"];
$sql="SELECT * FROM `posts` as p,`circles` as c WHERE c.circle_create_id=$id and (p.with=c.id or p.with=1)";//p.with columns contain circle id and as i tell first entry will be public
$sql_fier=mysqli_query($sql);
/*-------I think you know how to manipulate fetched data---------*/
Connect me on social network http://www.funnenjoy.com (signup/login is required )