want to find the total of two columns in PHP - php

Want to find the total of two or more table columns

Use GROUP BY and then SUM(condition) to obtain your counts:
SELECT id AS ID,
SUM(total = 'total' ) AS Total,
FROM table
GROUP BY ID
Something like this should work.

Related

SQL Select the column if the value is like with multiple columns

I need to create a autocomplete search with ajax. The suggestions should only contain the 10 most entered results. The search query has to check multiple columns if the value is like my variable.
But my problem is to create the query and the php logic for that.
Is there any plugin or something simular for that?
How can I select a column if the value in it is like my variable?
I need to create a count query, which counts (in all columns) "how often is here the full word (splitted by spaces)" <- which is like the found one (to get the relevance)
At the end I need to sort the found entries by their relevance to provide the 10 most relevant entries.
(The real query checks for more columns than just 2, but for dummy reasons are 2 okay)
The query which selects the rows where the value is like...
select * from
(
(select department from entries where department like '%myVariable%')
OR
(select grade from entries where grade like '%myVariable%')
)
I think you know what I mean. Does anyone have any hints, suggestions, examples or useful links for me?
Thanks in advance!
Best regards,
FriaN
Why not use union all here?
select department from entries where department like '%myVariable%'
union all
select grade from entries where grade like '%myVariable%'
Then this should order the results for you:
select department, count(*) cnt from (
select department from entries where department like '%myVariable%'
union all
select grade from entries where grade like '%myVariable%')a
group by department
order by count(*) desc

SQL: Delete duplicated rows? (PHP)

I have the following database and want to delete the red ones because they are doubouled. So I have to check every row if another row is matching by pid, price, price_old, link and shop.
But how can I check that and how can I delete it then?
Maybe an easier way would be to generate a id from the values inside each row. So if the values inside a row would be equal also the id would be equal and who have only one value to compare with the other id's.
Is that a better way? - If yes, how can I do that?
Greetings!
Do the fact you have no way for get thi distinct row you could add uniqie id using
ALTER TABLE my_table
ADD id int NOT NULL AUTO_INCREMENT
Once done you could use not in where the id are not the min grouped by the value you need for define the duplication
delete from my_table
where id NOT in ( select min(id) from my_table
group by shop, link
)
The simplest way is to run a distinct query:
select distinct pid, price, price_old, link, shop
from t;
You can create a new table using into. That is the simplest way. Because all columns are the same, MySQL doesn't offer a simple method to delete duplicate rows (while leaving one of them).
However, it is possible that your current results are generated by a query. If so, you can just add select distinct to the query. However, it would be better to fix the query so it doesn't generate duplicates. If this is the case, then ask another question with sample data, desired results (as text, not an image), and the query you are currently using.
Test this first on a test table:
DELETE t1
FROM t t1, t t2
WHERE t1.id > t2.id AND t1.price = t2.price
AND t1.link = t2.link AND t1.shop = t2.shop
AND t1.price_old = t2.price_old;
Basically you are removing the one with the highest ID if those parameters are equal
select * from
(select pid, price, price_old, link ,
row_number() over(partition by pid, price, price_old, link, shop order by pid) as rank
from my_table) temp
where temp.rank = 1
This Query will group by all the columns first and rank them. Duplicate rows will have rank > 1. It does not matter we take first or second row as both are copy of each other. We just take rows with rank 1. Rows that are not duplicate will also be having rank 1 and hence won't be neglected.
One more way to this is by using union.
select * from my_table UNION select * from my_table

php select from 2 tables and order by

i have 2 tables:
1st table is called maxlift with 1 column called, exercise
2nd table is called workout with 5 columns called, exercise, sets, reps, weight, date
i want display the highest weight for each exercise where exercise in both tables is the same.
i am using:
`SELECT *
FROM workout, maxlift
WHERE workout.exercise = maxlift.exercise
GROUP BY maxlift.exercise
ORDER BY workout.weight desc`
the problem i have is the weight displayed is not the highest weight from table workout.
thanks for any help.
You can use the aggregate function max() for this.
SELECT workout.exercise, max(weight) as weight
FROM workout, maxlift
WHERE workout.exercise=maxlift.exercise
GROUP BY workout.exercise;
Now, you basically group by exercise and find the maximum weight for each such group using the max() function. You can alias your selected column using column_name as alias.
`SELECT *
FROM workout, maxlift
WHERE workout.exercise = maxlift.exercise
ORDER BY workout.weight desc`
use this by removing group by and let me know about it

MySQL get results with MySQL only

Say I had a MySQL table as pictured below, and I wanted to find any product_id's which have the filters 2 AND 5 (so in the table this would be the product_id of 30), is this possible in MySQL? Or would I have to get product id's where the filter_id is 2 OR 5, and sort it in PHP?
SELECT product_id, COUNT(*) AS i FROM mytable WHERE filter_id IN (2,5) GROUP BY product_id HAVING i = 2;
This will match any rows containing any of the required filter_ids and group together those with the same product_id. The name i has been given to a column giving the number of rows in the group. Assuming that every combination of product_id to filter_id will be unique then you know that any group with two rows will have matched both of the filter_ids, so the HAVING clause will finally filter out the rows that only matched one of the filter_ids.

Sorting count in multiple tables

I'm working on a PHP/mySQL table that shows data I've put into my database, and I'm trying to make it sortable. I have two tables in my database:
Table "restaurant" has columns: ID and name
Table "item" has columns: ID, name and restaurantID (restaurantID is set to use the IDs from the "restaurant" table)
What I want to do is sort the restaurants by the number of times their ID shows up in the item table. I'm sure there must be a simple way to do this, Just haven't been able to figure it out. Any help would be greatly appreciated!
Try this...
select r.name, count(i.ID)
from restaurant r
left join item i on i.restaurantID = r.ID
group by r.name
order by count(i.ID) desc
I believe you can do it by using a query like following;
SELECT restaurant.*, COUNT(items.id) AS item_id FROM restaurants, items WHERE restaurant.id = items.restaurant_id ORDER BY item_id ASC;
As you may know, sorting by multiple column is possible as well;
SELECT restaurant.*, COUNT(items.id) AS item_id FROM restaurants, items WHERE restaurant.id = items.restaurant_id ORDER BY item_id ASC, restaurant.`name` DESC;

Categories