2

This is my table

DB -Fiddle Link Also a SQL FIDDLE LINK

`id`, `pname`, `comapny_id`, `premium`
(1, 'aaaa', 1, '1'),
(2, 'bbbb', 1, '2'),
(3, 'cccc', 1, '3'),
(4, 'dddd', 1, '2'),
(5, 'eeee', 1, '1'),
(6, 'ffff', 1, '3'),
(7, 'gggg', 1, '3'),
(8, 'hhhh', 1, '2'),
(9, 'iiii', 1, '1'),
(10, 'jjjj', 1, '2');

I want to combine 3 queries.

I tried

SELECT *
FROM query_test 
WHERE company_id = 1 AND premium = 1 
LIMIT 3 
INNER JOIN query_test ON SELECT * FROM query_test WHERE company_id = 1 AND premium = 2 LIMIT 4;

But that does not work for me. How can I join the following three queries using SQL join? I am learning SQL join from W3Schools.

Query #1:

SELECT * 
FROM query_test 
WHERE company_id = 1 AND premium = 1 
LIMIT 3

Query #2:

SELECT * 
FROM query_test 
WHERE company_id = 1 AND premium = 2 
LIMIT 4

Query #3:

SELECT * 
FROM query_test 
WHERE company_id = 1 AND premium = 3 
LIMIT 3

How can I achieve that with minimum time consumption?

Help should be appreciated.

Gracias de antemano.

2
  • but ther is limit in all query Commented Apr 2, 2018 at 6:43
  • Please provide minimal reproducible example in the body of the question. LInks to external sites may not exist in the future. You should be able to create an example of the table(s) to include in the Question. Commented Apr 2, 2018 at 6:45

4 Answers 4

4

Try using LIMIT with UNION, with proper syntax:

(SELECT * FROM query_test WHERE comapny_id = 1 AND premium = 1 LIMIT 3)
UNION ALL
(SELECT * FROM query_test WHERE comapny_id = 1 AND premium = 2 LIMIT 4)
UNION ALL
(SELECT * FROM query_test WHERE comapny_id = 1 AND premium = 3 LIMIT 3);
Sign up to request clarification or add additional context in comments.

6 Comments

Not working . And Why The above answer is upvoted when it is not working
#1064 - You have an error in your SQL syntax;syntax to use near 't UNION ALL (SELECT * FROM query_test WHERE comapny_id = 1 AND premium = 3 LIM' at line 4
@WebsterTP My updated answer should work now, please try it.
Every derived table must have its own alias
@WebsterTP Hopefully you can see why so many people botched the syntax. We don't do this very often, and even some documentation can be unclear about it.
|
1

You can use UNION

UNION is used to combine the result from multiple SELECT statements into a single result set.

SELECT * FROM query_test WHERE comapny_id = 1 AND premium = 1 LIMIT 3
UNION
SELECT * FROM query_test WHERE comapny_id = 1 AND premium = 2 LIMIT 4
UNION
SELECT * FROM query_test WHERE comapny_id = 1 AND premium = 3 LIMIT 3;

3 Comments

@WebsterTP There is some issue with Fiddle, Above code works in MySql. Test once over there
I Have tried in my phpmyadmin that is bit work and gives above error\
Why dont you use sqlfiddle or dbfiddle given by questionner
1

The fastest way is to use UNION

    (SELECT * FROM query_test WHERE comapny_id = 1 AND premium = 1 LIMIT 3)
    UNION
    (SELECT * FROM query_test WHERE comapny_id = 1 AND premium = 2 LIMIT 4)
    UNION
    (SELECT * FROM query_test WHERE comapny_id = 1 AND premium = 3 LIMIT 3)

Comments

0

Try By this

SELECT * FROM 
    (SELECT * FROM query_test WHERE comapny_id = 1 AND premium = 1 LIMIT 2) AS G 
UNION ALL 
    (SELECT * FROM query_test WHERE comapny_id = 1 AND premium = 2 LIMIT 2) 
UNION ALL 
    (SELECT * FROM query_test WHERE comapny_id = 1 AND premium = 3 LIMIT 2)

Its working and tested.!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.