I have a table called Notes and column name as id and name
eg datas
Id name
1 DEV
2 Prod
3 Prod
4 Prod
5 SQL
From this above table I need to retrive the value like this
Id name
1 DEV
2 Prod
5 SQL
SELECT MIN(id), Name
FROM Notes
GROUP BY Name
Related
So im trying to get a table that shows 3 activities which have the id's of 6,7,8 and they must be from a specific country. It should show their names and the number of times they appear in the person_activity_interval but the table i get is only 1 row with the name of 1 activity and the count of all the activities that are listed. If i remove other 2 activities it gets me the correct name and count but i need a table with all 3. Do i need to use something like join or union?
SELECT name,count(activity_id)
FROM activity,person_activity_interval
WHERE oib IN (SELECT oib
FROM person
WHERE living_id IN (SELECT id
FROM living
WHERE country= "Poland"))
AND ((activity_id=8 AND id =8) OR (activity_id=7 AND id =7) OR (activity_id=6 AND id =6));
He's a beginner, and I've encountered this programming conundrum:
We have sample records in the table:
ID
VALUE
1
5
1
4
2
3
2
4
3
3
3
5
I would like to retrieve records with VALUE (3,4) values with the same ID, so as a result I would like to get ID 2
How to write such a mysql query to the database?
You can use HAVING.
SELECT id FROM table_name WHERE value IN (3, 4) GROUP BY id HAVING COUNT(*) = 2;
I have prepared an example for you to see how you work.
I have searched quite a lot but haven´t found a solution to my problem. Now I hope someone can help me here.
I have two tables in an MySQL database and both tables have the columns Name and ArticleID
What I would like to do is to copy the content in the column Name from Table 1 to Table 2 where the ArticleID match. All the names should be separated by a comma in the column Name in Table 2.
In Table 1 there are for example 3 rows with the same content in the column Name but each row has a unique ArticleID. In 3 other rows there are a different Name but the ArticleID´s is the same as the first 3 rows.
Table 1
Name 1 - 1
Name 2 - 2
Name 3 - 3
Name 4 - 1
Name 5 - 2
Name 6 - 3
Table 2
1 - Name 1, Name 4
2 - Name 2, Name 5
3 - Name 3, Name 6
This would not normally be a problem for me, But now there are multiple rows with the same ArticleID and i can´t seem to figure it out by my self.
I hope you understand what I want :-)
Melker
INSERT INTO TABLE2(id, names)
SELECT ArticleID, GROUP_CONCAT(Name)
FROM TABLE1 GROUP BY ArticleID;
UPDATE
TABLE2
JOIN (SELECT ArticleID, GROUP_CONCAT(Name) AS Name FROM TABLE1 GROUP BY ArticleID) TABLE1
ON TABLE2.ArticleID = TABLE1.ArticleID
SET TABLE2.Name = TABLE1.Name
WHERE TABLE2.ArticleID = TABLE1.ArticleID
I actually had to use UPDATE and this worked
I have the following two tables
# vendor table
vendor_id host
1 192.168.0.2
1 192.168.0.4
1 192.168.0.6
2 192.168.1.21
2 192.168.1.23
2 192.168.1.25
2 192.168.1.27
# information table
host name
192.168.0.2 bar
192.168.0.4 bar1
What I need at the end is the following result set
vendor_id amount_live amount_total
1 2 3
2 0 4
The column amount_live is the amount of entries per vendor in the information table and the column amount_total is the amount of hosts in the vendor table per vendor.
Can some of the experts please tell me the mysql select statement to get the desired result set. Thanks.
You can do this with count and an outer join:
select v.vendor_id,
count(i.host) amount_live,
count(*) amount_total
from vendor v
left join information i on v.host = i.host
group by v.vendor_id
SQL Fiddle Demo
I have a project in ruby on rails 3.0.I have a database schema in pg. I have the instance of this project on 2 servers with respective databases.Now I have to shift everything to one server.So how do I export data from one database to the other?It can not be a literal export-import of databases bcos it has many tables with id and many to many relationships.So basically I need to append it so that there s no conflict for example
Databse 1 table 1 user
id Name
1 Josh
2 Rajn
4 Kush
Database table 1 user
id Name
1 Ram
2 Kevin
7 Don
So the new should be
Databse table 1 user
id Name
1 Ram
2 Kevin
7 Don
8 Josh
9 Rajn
10 Kush
and the join tables should have the new ids too
Not down the maximum value of the id field from all the target tables and add them as the offset values to the source id fields.
Ex:
migration.sql
SELECT #max_user_id := MAX(id) FROM users;
SELECT #max_comment_id := MAX(id) FROM comments;
# Then perform the following mysql commands in the target database:
INSERT INTO target.users(id, name) SELECT id + #max_user_id, name FROM source.users
INSERT INTO target.comments(id, comment, user_id) SELECT id + #max_comment_id, comment, user_id + #max_user_id FROM source.comments
Note that you cannot do the migration in