MYSQL summing values of multiple tables - php

I have multiple tables, I can list their rows to my website.
What I want is where last column has same value, I want to add up rows values and list as one line.
What I have:
Hioss
AUR
Top
1
1
0
Shen
Hioss
AUR
Top
1
1
0
Shen
Kanani
AUR
Jungle
1
1
0
Reksai
I don't want to get data like this.
If last column has same values (Shen) I want to sum int values and show as one line at my website.
What I want to do:
Hioss
AUR
Top
2
2
0
Shen
Kanani
AUR
Jungle
1
1
0
Reksai
My mysql query:
mysql_query("SELECT * FROM table1 UNION SELECT * FROM table2");
How can I do it? What should I do?

Try this. i dont know the fieldnames. You must change it
SELECT fieldnam1,fieldnam2, sum(fieldnam3),fieldnam4
FROM (
SELECT * FROM table1
UNION
SELECT * FROM table2
) as result
GROUP by fieldnam4;

Related

Get dependent records from table

Here is the table structure. I want to fetch all the id_product which have value 2 and 601(here id_product = 5). If i use OR all the records will be populated which is not necessary.
id_product attribute_id Value
5 2 2
6 2 2
8 2 601
6 2 601
5 3 601
8 3 32
6 3 41
Any help would be appreciated. I don't want to use sub query :-p
You can use a group by query:
select
id_product
from
tablename
where
attribute_id=2 and values in (2,601)
group by
id_product
having
count(*)=2
this will select all products that have (attribute_id=2 and value=2) or (attribute_id=2 and value=601) in two different rows, and then it will count the number of rows returned and select only products that have two rows (one with value 2 and one with value 601).
Another option (it's not too clear from your question) is to use this where clause instead of the one on the query above:
where
(attribute_id=2 and value=2) or
(attribute_id=3 and value=601)
You can use this query in your case:
SELECT * FROM nameTable WHERE Values IN (2,601) and attribute_id = 2

MYSQL Select; I cannot figure out how to exclude rows based on a different column value

Table:
id value
100 1
101 1
102 1
102 0
103 1
I want the selection to return id's 100,101,103; that is, if the same id has two rows, one with value=1 and a second with value=0, I want to exclude it.
Appreciate any help.
Try this:-
Select id From table Group By id Having count(*) = 1
Could use the having MySQL construct:
SELECT id, count(*) AS tehCount FROM table GROUP BY id HAVING tehCount = 1;
Try this:
SELECT * FROM
table
GROUP BY id
HAVING COUNT(*) = 1

How can I select all the values of a database and choose one of them by a percent

What I want to do is to select a value of the database,
Lets say:
id ---- giftid ---- userid
1 1 481
2 1 422
3 7 123
4 9 542
5 1 122
6 1 455
For example, there are 4 users that want to have the same giftid:
1, 2, 5, 6
It means that each one will have 25% to be chosen.
How can I make the "percent selection"?
Assuming every userid can only claim a giftid once, you can use the ORDER BY RAND() in MySQL. This will firstly select all the rows from table table where the giftid is 1 and then the results are ordered randomly. The LIMIT 1 ensures that only the first record is returned
SELECT * FROM table
WHERE giftid = `1`
ORDER BY RAND()
LIMIT 1
Are you looking this?
SELECT giftid, 1.0 / COUNT(*) percentSelection
FROM tableName
GROUP BY giftid

PHP MySQL Joins

Is there a way I could somehow do the following?
Table ONE
id c_id type
-------------------
1 1 7
2 2 7
3 3 5
4 4 7
Table TWO
id title live
-------------------
1 row1 1
2 row2 0
3 row3 0
4 row4 1
The c_id column links the data from table ONE to table TWO. Example: in table ONE, if the c_id is 2, that row in table ONE will be directly linked to table TWO's row with id 2, which has a title of "row2".
I want to select from table ONE, everything with type 7, but only if their associated data in table TWO has live set to 1.
Here's how I thought I'd do it, but this doesn't seem to work:
SELECT * FROM ONE, TWO WHERE ONE.type='7' AND TWO.live='1' ORDER BY ONE.id DESC LIMIT 5
I would expect the above to return only rows 1 and 4 from table ONE, because although table ONE has three rows with type "7", only rows 1 and 2's associated row in table TWO have live set to 1.
You were close... try using an implicit join:
SELECT ONE.* FROM ONE, TWO WHERE ONE.type='7' AND TWO.live='1' AND ONE.c_id = TWO.id ORDER BY ONE.id DESC LIMIT 5
select * from one join two on c_id = two.id where type=7 and live = 1
order by one.id desc limit 5

how to build a dynamic runtime query in mysql php?

I do not know how to classify this question. Vaguely, its about using calculated value in the WHERE clause of a mysql query performed using a php script.
Here's the scenario -
I've a mysql table with structure like this: table_id[int], item_id[int], item_rating[int]
Now the item_rating column can have either a "1" or a "0" value in it. The table_id column is set to auto_increment and the item_id column can have duplicate values also.
So a typical table will look like below -
table_id item_id item_rating
1 item1 1
2 item5 0
3 item1 1
4 item1 1
5 item5 1
6 item1 0
What i intend to do i for each item_id, i count the number of item_rating = 1 and item_rating = 0 and then take the difference of item_rating values to get the final rating for that item (final_item_rating = item_rating(with value=1) - item_rating(with value=0) ).
Now the issue:
I have a php script that takes values from these tables, and then displays the item details ordered on the "final_item_rating" value - something like:
select * from table_name order by final_item_rating desc
only problem is, since this final_item_rating is not a column in itself, and is actually based on run time value of the query, how do i build a query?
hope i have the question clear :)
This query may help you:
SELECT sum(item_rating) AS SumRatings
FROM table_name
WHERE item_rating=1
GROUP BY item_id
ORDER BY SumRatings;
This query would sum the ratings, and order the result with the highest rating on top:
select item_id
, sum(case when item_rating = 1 then 1 else -1 end) as rating
from YourTable
group by
item_id
order by
sum(case when item_rating = 1 then 1 else -1 end) desc

Categories