Mysql self join in View - mysql

I am facing issue related to One to Many relation MySql Table.
Table: Employee {Columns: Code, Email, Supervisor(e.g. Code1, Code2, Code3), Hr1(Self-join with the employee), Hr1(Self-join with the employee)}
My Requirement is: We want to send birthday mail to all employees for the current date and that mail also we want to send employee supervisor and Hr with CC and BCC respectively.
So I want to create one view to fetch all the email ID linked with the employee just like the supervisor(BCC: Test#gmail.com) and HR(CC: Test#gmail.com).
So how to self-join the supervisor and Hr to achieve the Email Id in One row.
View ==> ViewEmployee : {Columns : EmployeeEmail, SupervisorEmail, HREmail }
So no need to fetch all the table field.
Please help me anyone know this.
Thanks
Sitansu

Related

Joining 2 tables together and using the where function based on a separate mysql query

I am building a training platform for work. I have created the requirements for a user to be trained based on a role given to them. If that role is aligned to a document it will sit against the user. I have managed to get most of the way but am struglling on the best way to finish the where statement within mysqli.
tbldocfiles is a list of my files. I am looking at docid (could be multiple files associated to the document)
tbltrainingaccess sets the roles (driver, warehouseman, customer services) and shows which role (by id) is associated to the document in docfiles.
tblusertraining is the list of users and what role they have associated to them. (driver, warehouseman, customer services).
I am listing the documents associated to the user so have thought the following is the best way:
Look at the user and how many roles he/she is allocated
Look at the roles returned in point 1 (where function)
Identify and match the documents that have the same roles as the user (Join function)
create the list, then look at the unique values for docid. (distinct value)
Example User Bri has the driver and warehouseman role.
There are 5 documents in the db, 3 of them are associated to the driver role (docid 1,2,3) and 2 of them are associated to the warehouseman role (docid 2,4) the 5th document is associayted to customerservice.
My query should do this:
List all documents associated to the roles, that are associated to the user Bri
1
2
3
2
4
Now select unique values (using docid) from the above list:
1,2,3,4.
So my answer will be a used as a count function at the end using mysql_fetch_rows
SELECT DISTINCT tbldocfiles.docid FROM tbldocfiles LEFT JOIN tbltrainingaccess ON (tbldocfiles.docid = tbltrainingaccess.docid) where groupid='1' or groupid='9'
The above code works. but i've got myself confused.
The where statement needs to be the result of a query similar to :
select * from tblusertrainingrole where userid='1' (1 will be a variable based on page selection)
the result in this would be 1, 9 which are the groupid results.
Basically any help would be appreciated! I am sure it will be simple but have burnt myself out on this for a while and most answers in here helped with joining but not the where statement (that I could find)
Thank you in advance everyone!
You can do a select statement in the where. Since it is an or statement you can use in for the results. Please replace * with the column name for the value you need. Should look like
where groupid in (select * from tblusertrainingrole where userid = '1')

How to create a view?

I'm pretty new in App Maker. I want to create an app that will collect various types of request (failures, new ideas, orders and so on ). For each type of request will be separate data model. Every data model (request) contains 3 the same information: date, applicient, comments.
In addition to the dashboard's stand for each type of request, I want to make one in which all entries will be displayed with only repating records and type of request as one more record (date, applicient, comments, type of request)
I think that Calculated Model is the answer here, but despite getting acquainted with the documentation, I don't know how to implement this in my case. Could anybody halp me with this ?
Below I am presenting the display of the above description. Records 1, 2, 3 ... presents records that don't replicate in another data model.
IMAGE:
https://drive.google.com/file/d/1gi6ylZacOVSkcqtpaupRpIOrzbq7fOsT/view?usp=sharing
I tried to do relations, but I couldn't displey this in one table, what is my goal. How to conigure the SQL datasource to do this ?
You can create a new calculated SQL model with UNION:
(SELECT 'Failures' AS REQUEST_TYPE, C.* FROM Failures AS C)
UNION ALL
(SELECT 'New Ideas', C.* FROM `New ideas` AS C)
UNION ALL
(SELECT 'Orders', C.* FROM Orders AS C);
You must have corresponding fields in your datasource that match the sql column names: REQUEST_TYPE, Date, Applicient, Comments
Reference:
Cloud sql model

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.

If multiple condition occurs then fetch rows from table based on first one among them in MYSQL

Table:
Login
[user_id,type]
{1,5}
{2,1}
Personal
[user_id,first_name,last_name,city_name]
{1,x,y,ohio}
{1,x,y,new_york}
{2,p,q,new_york}
Working
[city_idcity_name]
{1,ohio}
{1,new_york}
SQL:
select lgn.user_id, ps.first_name, ps.last_name, wrk.city_name
from Login lgn, Personal ps, Working wrk
where wrk.city_name = ps.city_name and ps.user_id = lgn.user_id and lgn.type = 5
Problem:
In the above table you can see a user "x" having two different address and my query is based on matching city_name with Working and Personal Tables,
still there are two addresses available so it should fetch details based first coming city_name
How can we do that in MYSQL..?
Help me with it..
Thanks in Advance..

How can I see full order details for each time a certain coupon code was used in magento?

We are trying to collect data on each person that used a certain coupon code "NEWCUSTOMER". We are trying to get the order details including their name, email address, and what they ordered.
Is the rule_id connected to an order in any way in the database? The magento databases don't seem to be all that friendly when you are trying to write your own mySQL statement to figure this information out.
Thanks!
This is similar to your previous question which i have also answered for you: Find the name and email address of non-members who used coupon code in Magento
The coupon code used on an order is actually a property of the order: coupon_code
It sounds from your question that you are directly querying the db, if so then you are looking for the coupon_code field in the sales_flat_order table.
Here is the sql:
SELECT `customer_firstname`, `customer_lastname`, `customer_email` FROM `sales_flat_order` WHERE `coupon_code` = 'your_awesome_coupon_code' AND `customer_group_id` = 0
Or, via magento...
$orderCollection = Mage::getModel('sales/order')->getCollection()
->addAttributeToSelect('customer_firstname')
->addAttributeToSelect('customer_lastname')
->addAttributeToSelect('customer_email')
->addAttributeToSelect('customer_group_id')
->addAttributeToSelect('coupon_code')
->addAttributeToFilter('customer_group_id', Mage_Customer_Model_Group::NOT_LOGGED_IN_ID)
->addAttributeToFilter('coupon_code', 'NEWCUSTOMER');