I'm using PHP/MySQL and I have to create a new table from an existing one. Here's the problematic:
Table1:
photo_id email name_to creation_date link
1 foo1#bar.com paul 2012-11-21 link1.com
2 foo2#bar.com mark 2012-11-22 link2.com
3 foo1#bar.com alex 2012-11-23 link3.com
4 foo1#bar.com saul 2012-11-25 link4.com
5 foo1#bar.com john 2012-11-26 link5.com
6 foo2#bar.com math 2012-11-27 link6.com
7 foo3#bar.com fred 2012-11-28 link7.com
In Table1 the email is not unique, it can be repeated several times. Each link is different. With this data, I have to create a new table in which the email is unique with maximum of 3 links if there's more entries of one email (if so I need the data of the 3 latest entries).
So, in this case, Table2 would be like:
email link1 creation_date1 name_to1 link2 creation_date2 name_to2 link3 creation_date3 name_to3
foo1#bar.com link5.com 2012-11-26 john link4.com 2012-11-25 saul link3.com 2012-11-23 alex
foo2#bar.com link6.com 2012-11-27 math link2.com 2012-11-22 mark
foo3#bar.com link7.com 2012-11-28 fred
I know the GROUP_CONCAT feature but it's not really what I need here since the links would all be in the same column. Is it better to make a SELECT * FROM table1 and process the result into PHP arrays and after that create Table2 or a unique MySQL query would do the trick? Or create multiple MySQL tables?
Table1 is over 10 millions rows.
Any advice would be appreciate.
Thanks.
1) select all unique emails.
2) For each email, take the first 3 rows with that email ordered by creation_date descending.
3) Use that data to insert into new table.
Whatchu think?
Related
I'm looking to query a table that contains messages, organised with a sender and receiver. I would like to create a query to only return each combination of sender and receiver once. I currently have data stored in a database like so:
sender
receiver
1
2
2
1
3
4
4
3
The output I am looking for is as so:
1 | 2
3 | 4
Is this possible? If so, any hints in the right direction would be much appreciated.
One option uses least() and greatest(), and distinct:
select distinct least(sender, receiver) as user1, greatest(sender, receiver) as user2
from mytable
This question already has answers here:
What's the difference between INNER JOIN, LEFT JOIN, RIGHT JOIN and FULL JOIN? [duplicate]
(3 answers)
Closed 8 years ago.
I have 3 tables - User table, book1 table, book2 table.
User table is like this -
user_id | gender | l_name | f_name
-------- -------- -------- -------
1 male Doe Jon
2 female Xu Jini
3 female Din Jane
book1 table -
b_id | user_id | amount | date
----- --------- -------- ----------
1 3 98.30 2014-05-14
2 1 65.70 2014-05-07
3 2 14.40 2014-05-06
4 2 55.60 2014-05-07
book2 table -
b_id | user_id | amount | date
----- --------- -------- ----------
1 2 38.20 2014-04-06
2 3 84.40 2014-04-02
3 3 31.30 2014-04-12
4 1 74.40 2014-05-06
The user gives a date range as input and I want to calculate the sales count(COUNT), total amount(SUM) and the max date(MAX) for that date range. After this I want to connect this data to the user table and get the gender and name using the user_id.
I wrote this query to get the data for the given date range from book1 and book2 tables-
SELECT * FROM book1
WHERE date between '2014-04-02' and '2014-05-15'
UNION ALL
SELECT * FROM book2
WHERE date between '2014-04-02' and '2014-05-15'
ORDER BY customer_id;
By this i get all the rows in the book1 and book2 table which satisfy the date range. Now should i use subquery or something else to reach the goal. I think sql should take care till getting the count, sum and max from book tables. Then the connection to the user table should be done in PHP. Am i on the right path? Can everything be done in SQL? I am kinda lost.
Yes, you can do it in SQL using a plain JOIN.
This will basically get all users and join them up with their respective amounts in the period. After that, the results are grouped by user so that we can sum up the amounts.
SELECT u.user_id, u.l_name, u.f_name, SUM(x.amount) `total amount`
FROM user u
JOIN (
SELECT user_id, date, amount FROM book1
UNION ALL
SELECT user_id, date, amount FROM book2
) x
ON u.user_id = x.user_id
AND x.date between '2014-04-02' and '2014-05-15'
GROUP BY u.l_name, u.f_name,u.user_id
An SQLfiddle to test with.
As a side note, learning about joins is really a necessity to work efficiently with SQL databases.
I need a php routine for my mysql database. I have made an example to illustrate the problem:
Lets say I have a table that registrates customers and how much money they spend. A customer can have more registrations:
Table1:
Name - Amount
Jane - 3
Mark - 4
Sara - 5
Jane - 5
Jane - 6
Sara - 2
I want a routine that goes trough Table1, and finds how much each person has spend. I want the result in Table2, like this:
Table2:
Jane - 14
Mark - 4
Sara - 7
Do you have a solution to this?
insert into table2 select name, sum(amount) from table1 group by name;
SELECT Name, SUM(Amount) FROM Table1 GROUP BY Name
This is an expansion of my original question located here:
How do I pull all rows from a table with one unique field and specific values for another field?
I have a table with two fields: user_id and skill_id.
I want to pull out all rows that have a skill_id of a certain number but I have a large number of skill_id's to search for (~30). I was using the self-join suggestion presented in the question linked above but with so many skills to look for, that query is proving extremely slow.
How can I look for a large number of skill_ids without bogging down the query?
EDIT:
Here's an example of what I'm looking for. Using the table below, I want to pull out all rows of users that have skill_id of 10 AND 11 AND 12, etc. (except I'd be looking for more like 30 skills at a time).
TABLE
user_id | skill_id
=====================
1 | 10
1 | 11
1 | 12
1 | 13
2 | 10
2 | 12
2 | 13
3 | 15
3 | 16
4 | 10
5 | 45
5 | 46
If I understand your question well, below query might help you. Assuming (user_id, skill_id) is UNIQUE or PK.
SELECT user_id
FROM tab
WHERE skill_id IN (30 entries)
GROUP BY user_id
HAVING SUM(skill_id IN (30 entries)) = 30;
You can test here. http://www.sqlfiddle.com/#!2/f73dfe/1/0
select user_id
from table
where skill_id IN (10,11,12...)
make suer skill_is is indexed
I have a database that has id numbers, names, class period, and teacher name. Each row includes a student's id, name, period, and teacher which means that there are multiple rows for each student. That's not my problem. My problem is that some classes have two teachers listed...which means there are two separate rows for 1 class period, one for each teacher. Unfortunately I have no control over the formatting of the data since it's exported from a system that doesn't have a lot of formatting options.
Here is an example, notice how rows 23 and 24 are for the same class period and student, but the only thing different is the teacher names.
pk id last first period teacher
14 12345 Smith John 3 HARRIS
15 12345 Smith John 8 LEAL
17 12345 Smith John 1 HOUSTON
23 56789 Doe Jane 8 MERCER
24 56789 Doe Jane 8 RUIZ
25 56789 Doe Jane 3 BECK
26 56789 Doe Jane 1 STEED
I would like to combine the two rows with the same period number and student name into one row. All the information would remain the same except the two different teacher's names would be combined into something like "Mercer & Ruiz." Ideally the final result would look something like,
24 56789 Doe Jane 8 MERCER & RUIZ
Is this possible using PHP and/or MySQL? I'm not looking for anyone to write the entire code or anything. I just can't seem to think of a way to accomplish it. I'd be happy with any direction/indication of a way to go about this.
As always, thanks for your time and help.
SELECT MAX(pk),
ID,
`last`,
`first`,
GROUP_CONCAT(teacher SEPARATOR ' & ') teachers
FROM tableName
// WHERE clause here...
GROUP BY `last`, `first`, `Period`
// ORDER BY clause here....
SQLFiddle Demo
SOOURCE
GROUP_CONCAT()
SELECT pk,id,last,first,period GROUP_CONCAT(teacher)
FROM table
GROUP BY id, first,last,period
Should do what you need.