Select all type of data - mysql

My table is like:
id name seller product
1 bags s1 gold bag
2 shirt s1 shirts
3 shows s1 big show
4 jewellery s1 jewellery
5 Tv s2 Tv
I want to select 4 rows and in 4 rows must have seller s1 and s2 both results. Right now when I selecting 4 records so first 4 record are selecting that have seller s1
Edit
I have tried "GROUP BY" but its only returning single result.... I mean id=1
And
select distinct seller from table
getting only only 2 rows
I need
"I want to select 4 rows and in 4 rows must have seller s1 and s2 both results."

select distinct seller from table
though this will return only 2 rows

The following solution will return 4 rows which contain both sellers s1 and s2.
Code Segment 1
The code segment returns only 3 records for "s1". This is accomplished by adding the where seller = "s1" and limit 3 clause to the query.
select id, name, seller, product
from mytable
where seller = 's1'
limit 3
The union clause joins the two code segments together.
Code Segment 2
The second code segment performs a subquery similar to what was seen in the first code segment. The outer query was added to ensure the limit clause only restricted to the subquery and not to the entire result.
select *
from (
select id, name, seller, product
from mytable
where seller = 's2'
limit 1) t1
Here is the full code with link to SQL Fiddle demo.
select id, name, seller, product
from mytable
where seller = 's1'
limit 3
union
select *
from (
select id, name, seller, product
from mytable
where seller = 's2'
limit 1) t1

Related

select all rows with lowest product_id

i have this table and i want to select all rows with product id 105. i can easily do this if i know product id and put in where clause but i dont know product id and it increases once this id work is completed it will delete these rows and search for next lowest product id.
with order bt product_id i can do but how to ensure that all rows are listed. if i will give 100 limit then first 10-15 may be lowest after that it will start listing next higher product_id rows
here is table
id name product_id some_columns
1 dff 105 gfff
2 fg 109 ffgfgf
3 tt 106 gttytt
4 tt 105 trtr
5 trr 112 trrrt
6 rrr 111 rttttr
7 ttyt 108 ttrtrtr
8 rrrr 105 rrerer
SELECT id, name, product_id, some_columns
FROM table_name
WHERE product_id = (SELECT MIN(product_id) FROM table_name)
here you can see that lowest product_id is 105 but i dont have any control on how many times it will appear in table. in some case it may be 10 rows and in some case it may be 250 rows.
so order by product_id will not work. as giving limit will list all id initial few rows may be 105 but after than when all 105 rows are listed it will start listing all rows which is higher than 105
the best solution would be if i could use where product_id=105 but my bad luck i dont have any control on product id so cant use product id in where clause. next big problem is i want to use it efficiently so we have indexed product_id column
i was exploring min value option but i am highly doubtful about its efficiency and probable affect on mysql
so any help will be great
You can try something like
SELECT id, name, product_id, some_columns
FROM table_name
WHERE product_id = (SELECT MIN(product_id) FROM table_name)
As far as I understood you want to select all the rows that match the minimum product_id. Be it 101, 102 or whatever, it's uncontrollable, so you get the minimum product_id in the row and then you select the rows that has the current minimum product_id.
You could try using a subquery for min_id group by product
select m.*
from table_name m
inner join (
select min(id) min_id, product_id
from table_name
group by product_id
) t on t.min_id = m.id

select records as a single with different queries

I have a table, the table structure of which is as follows
id service_type pincode
---------------------------------
1 B 695582
2 D 676102
3 P 685584
4 B 685608
I will get two different pincodes from the front end. say for example 695582 and 685608. I need to get the service_types at these pincodes with single query and the results should be a single row. I have tried a UNION query, but the results are in two different row.
select 'buyer' name, service_type from tm_location_carrier_lnk where pincode=695582
UNION
select 'seller' name, service_type from tm_location_carrier_lnk where pincode=685608
The result of the above query is as follows
name service_type
------------------
buyer B
seller B
How can i get results in a single row as follows
Buyer Seller
B B
Why not doing
SELECT (select service_type from tm_location_carrier_lnk where pincode=695582 LIMIT 1) as Buyer, (select service_type from tm_location_carrier_lnk where pincode=685608 LIMIT 1) as Seller;
Here you can find a working example: http://sqlfiddle.com/#!9/e01426/1
edit: added LIMIT 1 for avoiding duplicated pincodes.
One solution assuming unique pincodes and specific to your request:
Select
(select max(service_type)
from tm_location_carrier_lnk
where pincode=695582) as Buyer
, (select max(service_type)
from tm_location_carrier_lnk
where pincode=685608) as Seller;

Guidance required for sql query

I have a database with one table as shown below. Here I'm trying to write a query to display the names of medication manufactured by the company that manufactures the most number of medications.
By looking at the table we could say the medication names which belongs to the company id 1 and 2 - because those company manufactures the most medication according to this table, but I'm not sure how to write a query for selecting the same i said before.
ID | COMPANY_ID | MEDICATION_NAME
1 1 ASPIRIN
2 1 GLUCERNA
3 2 SIBUTRAMINE
4 1 IBUPROFEN
5 2 VENOFER
6 2 AVONEN
7 4 ACETAMINOPHEN
8 3 ACETAMINO
9 3 GLIPIZIDE
Please share your suggestions. Thanks!
Several ways to do this. Here's one which first uses a subquery to get the maximum count, then another subquery to get the companies with that count, and finally the outer query to return the results:
select *
from yourtable
where companyid in (
select companyid
from yourtable
group by companyid
having count(1) = (
select count(1) cnt
from yourtable
group by companyid
order by 1 desc
limit 1
)
)
SQL Fiddle Demo
This Query might work. I have not tested but the logic is correct
SELECT MEDICATION_NAME
FROM TABLE where
COMPANY_ID=(SELECT
MAX(counted)
FROM ( SELECT COUNT(*) AS counted FROM TABLE ) AS counts);

How to combine the outcome of query?

I have a table about metro card of station enter and record record(cid, enter_sid, exit_sid)
I want to get the total number of enter and out each station.
For example,
cid enter_sid exit_sid
1 1 2
1 1 2
1 2 3
2 2 1
I want to get
sid count(*)
1 3
2 4
3 1
I don't know how to combine select cid, count(*) from record group by enter_sid and select cid, count(*) from record group by exit_sid
cid means id of card.
For the first row of my expected outcome, 1 is for the id of station, 3 is for sid 1 existing 2 times in enter_sid and 1 time in exit_sid.
the trick to this is your enter and exit sid are the first column so you have to union those two together to get the correct combination... from there its a simple sum of the count.
SELECT sid, cid, SUM(counting) FROM
(
SELECT cid, enter_sid as sid, COUNT(*) as counting FROM record GROUP BY enter_sid
UNION ALL
SELECT cid, exit_sid as sid, COUNT(*) as counting FROM record GROUP BY exit_sid
)t
GROUP BY sid
Working Fiddle

Is there anyway to perform a Count() when using Min()?

The statement below is to get all non-duplicate IDs from the products table. Is there a way I can get the total count of rows that are outputted by this sql statement?
select min(product_id) from products
where market_code = 'germany'
group by product_code
sample table data:
product_id market_code product_code
1 uk AAA
1 uk AAA
1 uk AAA
2 germany BAA
2 germany BAA
3 uk BAA
Thanks
You can simply do this:
SELECT COUNT(*)
FROM
(
select min(product_id) from products
where market_code = 'germany'
group by product_code
) AS t;
Actually your query is not what you said "The statement below is to get all unique/non-duplicate IDs from the products table." , It will select lowest product_id for product_code , not unique , for example if there are product_id - 2 and product_id - 3 for product_code A , it will only return product_id - 2 , it is a minimum value not unique , if you want unique you could do this way
SELECT product_code,product_id,count(*)
FROM TABLE
GROUP BY product_code,product_id
if you want just unique/non-duplicate ID-s and their count you can select with this query
SELECT product_id,count(*)
FROM table
GROUP BY product_id
The statement:
select min(product_id)
from products
where market_code = 'germany'
group by product_code;
is going to have as many rows as there are product code values in Germany. If you assume that a given product id never has two codes (which is true of your sample data), then the following counts the products in Germany without using product id at all:
select count(distinct product_code)
from products
where market_code = 'germany';