SQL code for conversation messages like facebook [closed] - mysql

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
tbl_messages:
m_from----m_to----m_message----m_date--------m_time1000------1001----hello-------------2013-02-01----12:11:111001------1000-----hi----------------2013-02-01----13:10:111000------1001-----how r u?-------2013-02-01----16:19:111001------1000-----fine------------(2013-01-26)---12:11:111002------1003-----ur age?---------2013-03-10--13:14:111003------1002-----25----------------2013-03-11--13:36:151002------1000-----ur name?-------2013-02-04--13:52:441002------1000-----rihanna----------2013-05-15--13:11:541000------1002-----im there---------2013-02-01--13:34:111000------1003-----im here----------2013-02-01--13:04:00
For example user(1000)=Michael wants to see his messages.
Michael should only see users that send or receive message.
user(1000)=michael===messege send or receive====> user(1001)
user(1000)=michael===messege send or receive====> user(1002)
user(1000)=michael===messege send or receive====> user(1003)
I want SQL code that only users shows that Michael has a message exchange with them. The result be with last message (send or receive) ORDER BY (first) m_date (second) m_time
example: (this result is for user(1000))
user(1001)=david----------------------------------------------------time=16:19:11send:slice of message(how r...)-----------------------------------date=2013-02-01user(1002)=jenifer----------------------------------------------------time=13:11:54received:slice of message(rihanna...)----------------------------date=2013-05-15user(1002)=tom--------------------------------------------------------time=13:04:00send:slice of message(im here...)----------------------------------date=2013-02-01

Supposing a table tbl_users with fields u_id and u_name.
Something along the lines of
(SELECT m_from, u_name, u_time, 'sent', m_message, m_date
FROM tbl_messsages inner join tbl_users on tbl_messages.m_from = tbl_users.u_id)
UNION
(SELECT m_to, u_name, u_time, 'received', m_message, m_date
FROM tbl_messsages inner join tbl_users on tbl_messages.m_to = tbl_users.u_id)
ORDER BY m_date, m_time;
Now, MySQL can do fancy things for formatting, but if I read your examples correctly, it can't put them like that, most likely, and you don't want to. A database (MySQL and others) is not made to do complex format transformation, but to store and retrieve information. The formatting can be done outside in another language.

Related

How can i get this SQL on yii2? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I am working with Yii2 and I need to use the Query Builder to convert the following raw SQL query where I am using a subquery in the inner join.
SELECT *
FROM class
INNER JOIN
(SELECT name, MAX(score) AS Maxscore
FROM class
GROUP BY name) topscore
ON class.name = topscore.name
AND class.score = topscore.maxscore;
You should show a bit of effort on your behalf as it is how it works here on SO, but as you are a new bee so i am adding an answer.
It is pretty straight forward see the Query builder guide, and you need to use the subquery in the following way
$subQuery = new \yii\db\Query();
$subQuery->select([new \yii\db\Expression('[[name]], MAX([[score]]) as Maxscore')])
->from('class')
->groupBy('[[name]]');
$query = new \yii\db\Query();
$query->select('*')
->from('class')
->innerJoin(['topscore'=>$subQuery])
->where(['=','class.[[name]]',new \yii\db\Expression('topscore.[[name]]')])
->andWhere(['=','class.[[score]]',new \yii\db\Expression('topscore.[[maxscore]]')])
->all();
Note: I didn't tested it thou but it should work correctly.

Converting E-mail to Names [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I am attempting to create a script or figure out a formula that will allow me to do the following.
I am a teacher and often have student use Google Forms for quizzes and different surveys. When grading it I normally split their email. Occasionally some students will have a number after their last name.
firstname.lastname#stu.county.stateschools.us
I would like to take their e-mail and convert it to the following format.
lastname, firstname
This would allow me to sort easily and put into gradebook much faster.
The current best route I know of to do this is to split via . , # then join the data I want.
This takes multiple different columns to complete my task that could very easily overwrite their data. I want this to all take place in one column and get rid of the extra information I do not need.
There are multiple ways to do that. I would probably start with the indexOf() "#"
slice() the string to get the "firstname.lastname"
Next, split() the string to separate the firstname and lastname
Now reverse() the order of ["firstname", "lastname"]
Finally, join() them back together
var email = "firstname.lastname#stu.county.stateschools.us";
var index = email.indexOf('#');
var name = email.slice(0, index).split('.').reverse().join(', ');
// Logs "The student name is: lastname, firstname"
Logger.log("The student name is: %s", name);

How to export leads history section in sugarcrm? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I'm using sugarCRM CE 6.5.14.
I would like to export leads history ( mail details) section. I googled but the details are/were not enough to complete my requirement.
Normal leads > export > not having this history section.
Here is the SQL Query that I used. It works for me.
SELECT l.first_name, l.last_name, l.id, group_concat(n.name), n.description
FROM leads AS l
inner join notes as n on l.id = n.parent_id
where l.deleted = 0 and n.deleted = 0
group by l.id
Hope this will help to some one like me.
There is no way of using the SugarCRM CE UI to export Email records. The closest you can get is to derive the URI index.php?module=Emails&entryPoint=export which will give you the data about an email record (e.g. subject, related to information, status). It won't provide the sender, recipient or email content.
That means you'll need to get into the database or use a GUI reporting tool like JasperReports.
A query that pulls all emails (meta-data and content) from the database would be
select
name,
to_addrs,
cc_addrs,
bcc_addrs,
description,
description_html,
parent_id,
parent_type
from emails
join emails_text on emails.id = emails_text.email_id;
/* where parent_id = '<your lead id>' */
You should know that the Lead History subpanel is populated two ways in regards to Emails. Emails can be directly related to a lead, i.e. parent_id = the lead guid. They can also show up there if the email address matches. This happens if an email is related to an Opportunity or a Contact, but the to_addrs or from_addrs contain an email address that matches an email address that is also on file with the Lead. The above query does not take this non-directly-related emails into consideration. For a query that'll help with that, I recommend you dig into include/utils.php and check out a function called get_unlinked_email_query().

meaning of a large mysql command [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Can anyone help by describing this php mysql command please:-
SELECT itm.*,mem.username as username,cpn.*
FROM aw_rdw_items itm,members mem,aw_rdw_items_coupons cpn
WHERE itm.item_id=cpn.item_id
and item_special_type != 'g'
and itm.item_id=cpn.item_id
and itm.memberid=mem.memberid
and item like('%".addslashes($_REQUEST["item_name"])."%')
$prs1 $prs2 $greenget $subsql
ORDER BY Display_Order,itm.item_id ASC
SELECT
itm.*, // all columns of table itm
mem.username as username, // column username and assign the values to a variable called username
cpn.* // columns of table con
FROM aw_rdw_items itm, members mem, aw_rdw_items_coupons, cpn // tables where execute the research
WHERE itm.item_id=cpn.item_id // various conditions
and item_special_type != 'g'
and itm.item_id=cpn.item_id
and itm.memberid=mem.memberid
and item like('%".addslashes($_REQUEST["item_name"])."%') // item must contain the specified variable
$prs1 $prs2 $greenget $subsql // some php variables
ORDER BY Display_Order, itm.item_id ASC // order by Dispay_Order and itm.item_id in ascendant mode
Return all records who item_special_type is different from "g" and item contains value from item_name request value.
Where clause has another conditions that contained at $prs1, $prs2, $greenget and $subsql variables.

sql select using same table for join [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
Using mysql 4/5
I have 2 tables:
ROUTE
route_order (int)
zipcode
LOADS
load_id
pu_zipcode
do_zipcode
Goal:
select all rows from Loads where
pu_zipcode and do_zipcode are in Route AND
where the route_order pu_zipcode < route_order do_zipcode
I think this is what you are asking:
SELECT L.*
FROM Loads AS L
JOIN Route AS R1
ON R1.zipcode = L.PU_zipcode
JOIN Route AS R2
ON R2.zipcode = L.do_zipcode
WHERE R1.ROUTE_ORDER < R2.ROUTE_ORDER
Try this query
SELECT l.load_id, l.pu_zipcode, l.do_zipcode
FROM LOADS AS l, ROUTE AS r1, ROUTE AS r2
WHERE l.pu_zipcode=r1.zipcode
AND l.do_zipcode=r2.zipcode
AND r1.route_order<r2.route_order