473,394 Members | 1,739 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Need help on a select statement using MYSQL ^.^

Hi,

I want to create a select statement showing -->

John -- happy -- Alice -- Mad -- Sunday
John -- Mad -- Alice -- happy -- Monday

Here is my table structure:

main (table)
main_id int int primary key
person_id_1 int references person(person_id)
mood_id int references mood(mood_id)
person_id_2 int references person(person_id)
mood_id int references mood(mood_id)
day char(10)

person (table)
person_id int primary key
person_name char(15)

mood (table)
mood_id int primary key
mood_type char(15)

I think my table structure setup is bad....thanks if anyone can help!
Aug 19 '05 #1
2 1211
Puffer Fish via SQLMonster.com (fo***@SQLMonster.com) writes:
I want to create a select statement showing -->

John -- happy -- Alice -- Mad -- Sunday
John -- Mad -- Alice -- happy -- Monday

Here is my table structure:

main (table)
main_id int int primary key
person_id_1 int references person(person_id)
mood_id int references mood(mood_id)
person_id_2 int references person(person_id)
mood_id int references mood(mood_id)
day char(10)

person (table)
person_id int primary key
person_name char(15)

mood (table)
mood_id int primary key
mood_type char(15)

I think my table structure setup is bad....thanks if anyone can help!


First of all, beware that if you stay in this newsgroup, you will
get solutions that will run on Microsoft SQL Server, and that may or
may not run on MySQL.

I can't reall tell whether your table definition is good or not,
because I don't know the business problem you are trying to solve.
Well "business problem" may not be the right world, because this does
not seem like business...

Anyway, this could do:

SELECT p1.person_name, m1.mood_type, p2.person_name, m2.mood_type,
m.day
FROM main m
JOIN person p1 ON m.person_id_1 = p1.person_id
JOIN person p2 ON m.person_id_2 = p2.person_id
JOIN mood m1 ON m.mood_id_1 = m1.mood
JOIN mood m2 ON m.mood_id_2 = m2.mood
ORDER BY CASE m.day
WHEN 'Sunday' THEN 1
WHEN 'Monday' THEN 2
...
END
--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp

Aug 19 '05 #2
Thanks Erland! It works


Erland Sommarskog wrote:
I want to create a select statement showing -->

[quoted text clipped - 20 lines]

I think my table structure setup is bad....thanks if anyone can help!


First of all, beware that if you stay in this newsgroup, you will
get solutions that will run on Microsoft SQL Server, and that may or
may not run on MySQL.

I can't reall tell whether your table definition is good or not,
because I don't know the business problem you are trying to solve.
Well "business problem" may not be the right world, because this does
not seem like business...

Anyway, this could do:

SELECT p1.person_name, m1.mood_type, p2.person_name, m2.mood_type,
m.day
FROM main m
JOIN person p1 ON m.person_id_1 = p1.person_id
JOIN person p2 ON m.person_id_2 = p2.person_id
JOIN mood m1 ON m.mood_id_1 = m1.mood
JOIN mood m2 ON m.mood_id_2 = m2.mood
ORDER BY CASE m.day
WHEN 'Sunday' THEN 1
WHEN 'Monday' THEN 2
...
END

--
Message posted via SQLMonster.com
http://www.sqlmonster.com/Uwe/Forums...neral/200508/1
Aug 20 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
by: John Fabiani | last post by:
Hi, I'm a newbie and I'm attempting to learn howto create a select statement. When I use >>> string1='18 Tadlock Place' >>> cursor.execute("SELECT * FROM mytest where address = %s",string1) All...
0
by: Fatt Shin | last post by:
Hi, I'm running MySQL 4.0.13, connecting from PowerBuilder 9 using ODCB Connector 3.51. I'm facing a problem where whenever I issue a SELECT COUNT(*) statement from PowerBuilder, I always get SQL...
0
by: B. Fongo | last post by:
I learned MySQL last year without putting it into action; that is why I face trouble in formulating my queries. Were it a test, then you would have passed it, because your queries did help me...
0
by: Eric B. | last post by:
Hi, I'm somewhat new to MySql. I've been using it for a while, but pretty much out of the box setup, and am starting to suffer heavily with my larger tables. I have a table with 5,000,000+...
2
by: Beyonder | last post by:
I have five tables in my database, there are actually NO common fields between them, not even a KEY or ID or anything like that, except for the "body" of a blob field. and that text is not...
4
by: Denis St-Michel | last post by:
Hello All, Hope some Guru will be able to help me with this. Let's take this example table A ------------------------------------------------------------------------------- id | ...
3
by: Hendry Taylor | last post by:
I have a problem where if I issue a select * from against a database it returns no data, but if I select column from it returns the data. Why would the * not be working as a wildcard?
17
by: kalamos | last post by:
This statement fails update ded_temp a set a.balance = (select sum(b.ln_amt) from ded_temp b where a.cust_no = b.cust_no and a.ded_type_cd = b.ded_type_cd and a.chk_no = b.chk_no group by...
2
by: JellyMan | last post by:
I have two tables in my Access database that I want to be able to synchronize by pressing a form button. I think it can work, but I am not sure how. I am trying to use a single query to do this. ...
2
by: scole954387 | last post by:
Hi, I have a problem. I have written a SQL statement that has a nested select case statement on the 'where' clause to condition the results. ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.