I've got 5 equal tables which look like this:
Table 1
| Name | Points |
|-------|--------|
| User1 | 5 |
| User1 | 2 |
| User1 | 5 |
| User2 | 1 |
| User2 | 7 |
| User2 | 9 |
The rows are inserted when a user answers different questions.
I also have the users in different table.
I want to:
echo User1: 12 points
echo USer2: 17 points
Ho can I make this?
Try this query
SELECT name, SUM(points) as total_points FROM table1 GROUP BY name
Related
i have 1 table in that i want to reorder the views column using alter function in my php file basically it should be reorder in most views to least views order.and want it to display in same order has altered but it is showing in order of id.
Now here i dont want to use ORDER BY views DESC thats why i am using alter function. because i am using ORDER BY name ASC already in my php file.
TABLE => users
id | name | views
1 | user1 | 700
2 | user3 | 900
3 | user1 | 200
4 | user4 | 900
5 | user4 | 800
6 | user4 | 800
7 | user3 | 900
8 | user4 | 900
9 | user5 | 100
10 | user5 | 100
// this is random table..//
ULtimately i am looking for that when by table is in views ORDERED then i will select data from table and if there are same name then it should be order in views as ALTERED so that i get ouptup like this :
id | name | views
1 | user1 | 900
2 | user1 | 700
3 | user1 | 200
4 | user3 | 900
5 | user4 | 900
6 | user4 | 800
7 | user5 | 900
8 | user5 | 900
9 | user5 | 300
10 | user5 | 100
SQL tables are unordered sets. They have no intrinsic order, and you should never rely on any order returned from queries that don't explicitly state an order by clause. In your case, it seems you need two elements in the ordering - first by name, and then by views:
SELECT *
FROM mytable
ORDER BY name ASC, views DESC
I have the following simplified table: (Note we skipped 2nd exam in the exam_id)
+----+---------+-------+
| id | exam_id | score |
+----+---------+-------+
| 1 | 1 | 15 |
| 2 | 1 | 20 |
| 3 | 1 | 68 |
| 4 | 3 | 92 |
| 5 | 3 | 10 |
+----+---------+-------+
I want to write an $sql and some php (I'm using Wordpress, and can use $wpdb) to be able to get the following:
$exam[3]=10
$exam[1]=68
Not that when there are multiple exams, we take the score entry which corresponds to the largest id
And $exam[2] is empty. In words, I'd like to save the last ever exam that the user attempted and show their score.
I've tried using Group By and
Try this
SELECT
*
FROM exams
WHERE exams.id IN (
SELECT
MAX(id)
FROM exams
GROUP BY exam_id
);
we have building a chat application using php and mysql and our table structure looks like this
converstation_table_name
id
conversation_id
created_at
user_1
user_2
messages
sender
reciever
message
created_at
conversation_id
here are the values in both tables
conversation_table_name
id | con_id(shortname) | created_at | user_1 | user_2 |
1 | 123132 | now | 1 | 5 |
2 | 123133 | now | 1 | 3 |
3 | 123134 | now | 1 | 4 |
4 | 123135 | now | 1 | 2 |
messages
sender | reciever | message | created_at | conversation_id |
1 | 3 | abc | now | 123133 |
3 | 1 | cee | now | 123133 |
1 | 2 | 1411 | now | 123135 |
1 | 5 | 1-5 | now | 123132 |
now we want to arrange the output something like this
123132
between 1 - 5
123135
between 1 - 2
123133
between 1 - 3
how to go about it, in short we want to make the latest message in user conversation to appear on top, just like facebook does with it's messaging system, how to go about it, we are still looking for a working answer
Can be introduced new field viewed or seen with datetime field. On visiting message update the field. So can be used order by that field and you will have the viewed time.
i'm new to MySQL and PHP. And i have some problems trying to get data values from two tables in one query using JOIN. What i want to do is query "user_builds" and SUM(amount) where the owner_id=1 AND type=1. The problems comes in now where i have to grab the build_type from another table called "builds".
I have tried to solve this as i mentioned with JOIN, but the closest i came was to get the amount of rows that was equal to how many rows user_id=1 had.
What i want is select the total SUM of "amount"(user_builds) where "type=1"(builds) and "owner_id=1"(user_builds).
I hope you understand what i try to do here, if not i will try to elaborate it more. And also sorry for not providing any of the querys i tried, but as none of them worked it feels irrelevant. Thank you for your time.
Edit:
+-------------------+
| user_builds |
+---------+---------+----------+-------+
| id |owner_id | build_id | amount|
+---------+---------+----------+-------+
| 1 | 1 | 1 | 5 |
| 2 | 2 | 2 | 15 |
| 3 | 2 | 3 | 15 |
| 4 | 1 | 4 | 5 |
| 5 | 1 | 5 | 5 |
| 6 | 1 | 6 | 10 |
+---------+---------+----------+-------+
+----------------------+
| build |
+---------+------------+-----------+--------+
| id | name |description| type |
+---------+------------+-----------+--------+
| 1 | House | desc | 1 |
| 2 | Kitchen | desc | 2 |
+---------+------------+-----------+--------+
I want to query "user_builds" and get the total of "amount" where owner_id=1 and type=1. (type is found in "build" table).
Try this code, I hope it works appropriately.
select sum(ub.amount)
from user_builds ub
left join build b
on ub.build_id = b.id
where b.type=1
and ub.owner_id = 1
select SUM(amount) from user_builds left join builds on build.type = user_builds.type where "owner_id=1"
try this query and replace my query field with your original fields
best of luck...
(Sorry, my english isn't very good)
Hi, I am trying to learn how to work with junction tables in MySQL and I can't figure how to do something. I know the basics of MySQL but I have never worked with "JOIN".
In this test project, I would like to be able to show on a page the app of a given category (you click on "Games", only the apps that are in the "Games" category will be displayed on the page). I would like to know what the SQL request should look like.
Second question, let's say that an App could fit 2 different categories, how can I manage to give that app 2 different Category_ID in my database ?
Here is what my Database looks like at the moment :
Table name: APPS
+------------+-------------------+
| App_ID (pk)| App_Name |
+------------+-------------------+
| 1 | Weather Network |
| 2 | Is it sunny 2.0 |
| 3 | The Weather App |
| 4 | Zelda |
| 5 | Megaman |
| 6 | Doom 3 |
+------------+-------------------+
Table name : CATEGORY
+-----------------+-----------------+
| Category_ID (pk)| Category_Name |
+-----------------+-----------------+
| 1 | Games |
| 2 | Weather |
+-----------------+-----------------+
Table name : JUNCTION_APP_CATEGORY
+----------------+--------------------+
| APP_ID (pk) | Category_ID (pk) |
+----------------+--------------------+
| 1 | 2 |
| 2 | 2 |
| 3 | 2 |
| 4 | 1 |
| 5 | 1 |
| 6 | 1 |
+----------------+--------------------+
For your first question, the answer is
SELECT a.*, c.*
FROM APPS a, CATEGORY c, JUNCTION_APP_CATEGORY ac
WHERE a.App_ID=ac.APP_ID
AND c.Category_ID=ac.Category_ID
AND ac.Category_ID=<category_id for category "Games">
For your second question, you can use both APP_ID and Categor_ID as the primary key of table JUNCTION_APP_CATEGORY(note NOT TWO pks, but use the two columns together as ONE pk). So that you can put data like this:
+----------------+--------------------+
| APP_ID (pk) | Category_ID (pk) |
+----------------+--------------------+
| 1 | 1 | <-- APP_ID=1 belongs to both cat 1 & 2
| 1 | 2 |
| 2 | 2 |
| 3 | 2 |
| 4 | 1 |
| 5 | 1 |
| 6 | 1 |
+----------------+--------------------+