Board index   FAQ   Search  
Register  Login
Board index php forum :: Database mySQL & php coding

Two columns from one in Mysql?

Codes here !

Moderators: macek, egami, gesf

Two columns from one in Mysql?

Postby puchu83 » Tue Jul 09, 2013 10:53 am

Hi guys, maybe this is a stupid question but It's my first step in mysql
enivroment...

I got a table like this
Code: Select all
Id|   Name|   Objects|      Class|
1      A         5            1
2      B         10           1
3      C         21           1
4      D         6            1
5      A         10           2
6      B         18           2
7      C         7            2
8      E         65           2

I would like to have a table in php like this:
Code: Select all
|Id|    |Name|   |Objects when Class1|   |Objects when Class2|
 1      A              5                               10
 2      B              10                              18
 3      C              21                              7
 4      D              6                               0
 

and so on....

My problem is that the in the column Name I have some elements with the same
name, but with different number of objects when considering different classes.

So, which is the query in Mysql to obtain on the same row different values coming from the same column but that can differ when there are more than one classes?
I hope it was clear, thanks in advance! :help: :help: :help:
puchu83
New php-forum User
New php-forum User
 
Posts: 2
Joined: Tue Jul 09, 2013 10:40 am

Re: Two columns from one in Mysql?

Postby johnj » Tue Jul 09, 2013 7:27 pm

For this, when you write your sql select query, you need to order the rows by the column that you are focussing, in this case it will the column called 'Name'.

Try something like this:
Select * from your_table ORDER BY Name, Class;
johnj
php-forum Super User
php-forum Super User
 
Posts: 1805
Joined: Thu Mar 10, 2011 5:07 pm

Re: Two columns from one in Mysql?

Postby puchu83 » Wed Jul 10, 2013 2:16 am

mmm ok but how can I have two classes on the same row?
puchu83
New php-forum User
New php-forum User
 
Posts: 2
Joined: Tue Jul 09, 2013 10:40 am

Re: Two columns from one in Mysql?

Postby johnj » Thu Jul 11, 2013 6:47 am

For that you will save all the necessary data to a temporary string and you will write the row ONLY when the field NAME changes. In that way, you will have class1 and class2 on the same row.
johnj
php-forum Super User
php-forum Super User
 
Posts: 1805
Joined: Thu Mar 10, 2011 5:07 pm

HOW TO ADD A MONTHLY COLUMN TO YEARLY COLUMN

Postby sreerajvt » Tue Sep 10, 2013 7:35 am

Hi guys,I am new in php & mysql. i have two tables. one is monthly and the other is yearly. The problem is, I want to add monthly data( integer) to yearly table. for eg. if a client is deposited 500 bucks every month,it will be added to his Yearly account . Please help me. thanks in advance.
sreerajvt
New php-forum User
New php-forum User
 
Posts: 1
Joined: Tue Sep 10, 2013 7:09 am


Return to mySQL & php coding

Who is online

Users browsing this forum: Baidu [Spider] and 1 guest

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.