Mysql sort BY number of filled columns [closed] - php

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I'm comparing prices of products from 3 different website. So i have 3 different prices. If website doesn't offer that product, its price is empty.
MySQL looks like this:
**id | name | price_1 | price_2 | price 3**
I would like to sort it from products, which are available at all 3 websites. To products, that are available at just one website.
And I can't figure out how to approach this!

If the empty fields have NULL value, you can use
SELECT * FROM sometable ORDER BY ISNULL(price_1) + ISNULL(price_2) + ISNULL(price_3) DESC;
But a more sensible solution would be:
You have one table, which contains the products
You have another
table, which contains the product's ID, the price and a value which
indicates which website provided that price.
Benefits:
You only have to COUNT(*) the entries of a product in the second table to find out on how many it is listed
You can change the number of websites any time without changing a table

Try this:
SELECT *, (IF(price_1 IS NULL,0,1) + IF(price_2 IS NULL,0,1) + IF(price_3 IS NULL,0,1)) AS odr FROM `test1` ORDER BY odr DESC

Related

MySQL: Insert list of 100 random numbers into one field then display them [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I have a table with 500 quotes, each one has an ID from 1 to 500. This table has 2 Columns: QuoteID and Quote.
A customer orders 100 random quotes on the Order page.
I need to generate 100 random numbers between 1 and 500 and somehow insert this into the Customer table for that customer’s order. The Customer table has 2 Columns: CustomerID and QuotesOrdered.
Then, on the Display page I need to display each of these quotes. It would have to look at the 100 random ID numbers listed in the QuotesOrdered column, then pull each of these from the Quotes table to display them.
Is there a simple way to do this? Would I generate the 100 numbers into an array, then somehow insert this array into the Customer table QuotesOrdered column, then go through the array and display each quote associated with that ID number from the Quotes table?
Any help on how to logically do this would be appreciated.
I am unsure why you would need to store random numbers in a table just to then select the quotes. You can directly select 100 random quotes with the following query:
select quoteID, quote from quotes order by rand() limit 100;
Now if, for some reason, you do need to persist 100 random quote numbers in the customers table for a given customer, then:
insert into customers
select :customerID, quoteID from quotes order by rand() limit 100
... where :customerID should be replaced with the id of the concerned customer.
Then when you want to display them:
select q.quoteID, q.quote
from customers c
inner join quotes c on c.quoteID = q.quoteID
where c.customerID = :customerID

MySQL - How to join two or more tables with more than one ID? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I am little new to MySQL and PHP. I am going to develop a database to a company who sell cosmetics products in different sizes. My database has two (2) tables. One is Products and other is Sizes. I am using PHP PDO. All I want is to join these two tables As I mentioned in the image. My question in very simply is,
How to insert and select products which has two or more sizes?
(If my this approach is wrong, please suggest me the proper one which can be used to another situations like this.)
Thank you in advance for your help.
My Table Map
you need to make another table name with product_size
with id,product_id,size_id
and you need to insert data to related it ilke
id prodict_id size_id
1 1 1
2 1 2
3 2 1
4 3 4
5 3 5
6 4 5
and so on
I have make this table on your desire out pot
then you need a query to get your desire output
select P.producr_id,P.name,group_concat(PS.size_id) from Product as P
join product_size PS on PS.product_id=P.id group by PS.product_id
I think you need to add one additional table for relations between products and sizes
CREATE TABLE `relations` (
`id_relation` int(11) NOT NULL AUTO_INCREMENT,
`n_product_id` int(11) NOT NULL,
`n_size_id` int(11) NOT NULL,
PRIMARY KEY (`id_relation`)
)

SELECT all rows by one field and different values [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
Please help me with this SQL Query.
I have a table "objects". In that table exist field agent. On web site my agent ID is "7".
also I have array with ID's of other agents.
I want to make one SQL query to SELECT all objects FROM objects WHERE agent=7 and also I want to select objects where agent = 4, 5, 8, 9 (id's 4 5 8 9 exist in my array "IDs")
NOW I have SELECT * FROM objects WHERE agent=7 AND status='$st' ORDER BY ID DESC but it select only my objects, I need include in this request another objects of another agents by their ids
You can use IN clause to fetch all the objects
SELECT * FROM objects WHERE agent IN ('4', '5', '7','8','9')
AND status='$st' ORDER BY id DESC
After reading your question a few times to actually get somewhere, I think you are asking to get a range of agents using their IDs.
You can do that by using SQL Code like this:
SELECT * FROM objects WHERE agent IN ('4','5','7','8','9') AND status='$st' ORDER BY id DESC
You could also use WHERE agent > 4
Use below SQL Query
SELECT * FROM objects WHERE agent IN ('4', '5', '7','8','9') AND status='$st' ORDER BY id DESC
All the best!

how to make this mysql query fast? I have millions of data inside my table [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I have a table called ad_view and it has 18.9 million number of data inside that table.
This is the table design of ad_vew
ad_view
-------
ad_id
network_id
publisher_id
landingpage_id
advertiser_id
I am using this query to get the ad_id and the count of ad_id depending on the network_id, so in this case the network_id is 4.
select ad_id,count(ad_id) as strength
from ad_view
where network_id = 4
group by ad_id
order by strength desc
limit 10
This query takes forever to load. Do you have any suggestion how to make this query fast?
I am not a database expert, if it takes to redesign this table, I would do that.
Any suggestion will be greatly appreciated, thanks!
This will help:
ALTER TABLE ad_view ADD INDEX (network_id, ad_id).
Make sure you have tuned your innodb_buffer_pool_size to hold the frequently-requested portion of your table in memory.
You might like my presentation, How to Design Indexes, Really. That presentation doesn't go into indexing for GROUP BY, but you can think of it like a range condition.
1 Create an index on network_id since you're searching by it
ALTER TABLE `ad_view` ADD INDEX (`network_id`);
2 If you're trying to get the count of ad_id for a given network_id, why do you need ad_id in your results? and why do you need to order by? I don't understand this. If all you want is how many ad_ids for network_id 4, then do:
SELECT COUNT(IF(network_id=4,1,null)) as strength from ad_view
It will return just a number. See this demo
PS: your initial post included a broken query (in the order by clause), which you changed after I made a comment. Your current query still doesn't give you what you say you want. I just tried it on this fiddle

Mysql Order by date multiple columns after joining two tables [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
Now i want like in 2015-08-04 the middle column of 2015-08-04 should be on same line order and 2015-08-05 should come down of both columns like if the both date matches they should be in same order like order by both columns its probably like ledger format my sql query
SELECT i.* , it.name AS type,td.qty AS issue, td.is_from_stock AS issue_qty_type,v.name AS vendor,o.name AS branch,td.date_from AS issue_date,l.name AS location,td.user AS user,td.remarks AS remarks, os.opening_quantity AS initial_stock,os.remaining_stock AS opening_quantity,os.opening_quantity_unit_price As opening_unit
FROM ". $this->tbl_inventories ." AS i
LEFT JOIN ". $this->tbl_inventorytypes ." AS it ON i.type_id = it.id
LEFT JOIN ". $this->tbl_distribution ." AS td ON td.item_id = i.item_id AND td.user_branch = i.user_branch
And i did like ORDER BY purchased_date,td.date_from
First of all, be bit specific on the image you have provided. I think its not possible to sort the data fetched from MySQL by the date directly as you have tried, since MySQL data output represents a single row for each set of data.
You have to breakdown to multiple query and put them in the respective arrays, so that you can filter out the data into final array i.e you have to rearange ta data into new array and display it to the table you have snapshot of.

Categories