How to select similar values together in same query? - php

I have the database as below, i need to get all the zeros and ones in separate list from the below table, that is to get all the zero together in a column and all the ones together in separate column
database
| id | value |
-------------
| 1 | 0 |
| 2 | 1 |
| 3 | 0 |
| 4 | 1 |
expected result
| sp.id | stop | st.id | start|
-------------------------------
| 1 | 0 | 2 | 1 |
| 3 | 0 | 4 | 1 |
or
| id | value |
-------------
| 1 | 0 |
| 3 | 0 |
| id | value |
-------------
| 2 | 1 |
| 4 | 1 |

SELECT a.id AS sp.id, a.value AS stop, b.id AS st.id, b.value AS start
FROM (SELECT * FROM TABLE WHERE = 0) a
LEFT JOIN (SELECT * FROM TABLE WHERE = 1) b
ON a.id = b.id
UNION
SELECT a.id AS sp.id, a.value AS stop, b.id AS st.id, b.value AS start
FROM (SELECT * FROM TABLE WHERE = 0) a
RIGHT JOIN (SELECT * FROM TABLE WHERE = 1) b
ON a.id = b.id

For your expected result 1, you can use case
select case when value=0 then id end as spid,
case when value=0 then value end as stop,
case when value=1 then id end as stid,
case when value=1 then value end as start
from yourtable.
But you will get NULL for empty rows as shown below. If that is fine, you can use the above query. If it is a String you can use MAX() or MIN() with group by to avoid this empty values.
OUTPUT
+------+------+------+-------+
| spid | stop | stid | start |
+------+------+------+-------+
| 1 | 0 | | |
+------+------+------+-------+
| | | 2 | 1 |
+------+------+------+-------+
| 3 | 0 | | |
+------+------+------+-------+
| | | 4 | 1 |
+------+------+------+-------+
For you expected output 2, you can directly use UNION ALL
select id,value from test where value=0
union all
select id,value from test where value=1
OUTPUT
+----+-------+
| id | value |
+----+-------+
| 1 | 0 |
+----+-------+
| 3 | 0 |
+----+-------+
| 2 | 1 |
+----+-------+
| 4 | 1 |
+----+-------+
CHECK DEMO HERE

Related

Query Select and Show All First Table And Second Table Even is not Exist in Second Table

SAMPLE TABLE
TABLE FIRST_TABLE
| rid | requirements |
| 1 | 2x2 pic |
| 2 | valid id |
| 3 | 137 form |
| 4 | app form |
Second table
| id | applicant_id | rid | remarks |
| 1 | 1 | 1 | pass |
| 2 | 1 | 2 | pass |
| 3 | 2 | 1 | pass |
How to select all records from first table and show even the data is not exist on second table.
Result should be like this.
applicant_id | rid | remarks |
1 | 1 | pass |
1 | 2 | pass |
1 | 3 | null |
1 | 4 | null |
this is my sample code.
select requirements from first_table
left join second_table on first_table.rid = second_table.rid
where second_table.applicant_id = 1
group by first_table.rid
//result :
applicant_id | rid | remarks |
1 | 1 | pass |
1 | 2 | pass |
You just need to move the second_table.applicant_id = 1 to the join.
select requirements, first_table.rid, remarks
from first_table
left join second_table on
first_table.rid = second_table.rid and
second_table.applicant_id = 1
group by first_table.rid
http://sqlfiddle.com/#!9/a0cffdd/17

left/inner join shows me 16 entries but i only have 4 in my database

can someone tell me why he shows 16 entries when i already have 4?
http://sqlfiddle.com/#!9/0d2300/7
i want to show all results where the domain_id is 1 or something else.
What am I doing wrong?
I want to include all 3 tables that I get this record 4 times beacuse in my domain_check table are 4 records
I want:
id | domain_id | ssl_check | ssl_orgname
1 | 1 | 1 | SSL_TELEKOM
1 | 1 | 0 | SSL_TELEKOM
1 | 1 | 1 | SSL_MEDIA
1 | 1 | 1 | SSL_MEDIA
If you want the LAST row from domain_check and assuming the id identifies last row then add a condition to the where clause
SELECT domain.id,
domain_check.domain_id,
domain_check.ssl_check ,
ssl_info.domain_id,
ssl_info.ssl_organisation
FROM domain
INNER JOIN domain_check ON domain_check.domain_id = domain.id
INNER JOIN ssl_info ON domain_check.domain_id = ssl_info.domain_id
WHERE domain.id = 1 and
domain_check.id = (select max(id) from domain_check s1 where s1.domain_id = domain_check.domain_id);
+----+-----------+-----------+-----------+------------------+
| id | domain_id | ssl_check | domain_id | ssl_organisation |
+----+-----------+-----------+-----------+------------------+
| 1 | 1 | 1 | 1 | SSL_TELEKOM |
| 1 | 1 | 1 | 1 | SSL_TELEKOM |
| 1 | 1 | 1 | 1 | SSL_MEDIA |
| 1 | 1 | 1 | 1 | SSL_MEDIA |
+----+-----------+-----------+-----------+------------------+
4 rows in set (0.001 sec)
I think this would help you
SELECT
domain.id,
domain_check.domain_id,
domain_check.ssl_check,
ssl_info.domain_id,
ssl_info.ssl_organisation
FROM domain
LEFT JOIN domain_check ON domain_check.domain_id = domain.id
LEFT JOIN ssl_info ON domain_check.domain_id = ssl_info.domain_id
WHERE domain.id = 1 group by domain.id
Here is the screenshot https://prnt.sc/uljgzx

Randomly select fields where few condition met

Here is the table-
+----+---------+--------+
| id | letters | status |
+----+---------+--------+
| 1 | A | 0 |
| 2 | B | 1 |
| 3 | C | 0 |
| 4 | D | 0 |
| 5 | E | 1 |
| 6 | F | 1 |
| 7 | G | 0 |
| 8 | H | 0 |
+----+---------+--------+
Its need to finds records with the conditions below-
select letters with LIMIT 3
ORDER is RAND()
status true or false both need to present but at-least a presents of
letters with status TRUE but not more then 2
Desire results could be as-
+---------+--------+
| letters | status |
+---------+--------+
| B | 1 |
| E | 1 |
| H | 0 |
+---------+--------+
+---------+--------+
| letters | status |
+---------+--------+
| C | 0 |
| E | 1 |
| H | 0 |
+---------+--------+
But not as-
+---------+--------+
| letters | status |
+---------+--------+
| C | 0 |
| G | 0 |
| H | 0 |
+---------+--------+
+---------+--------+
| letters | status |
+---------+--------+
| B | 1 |
| E | 1 |
| F | 1 |
+---------+--------+
Anyone please help.
Here is a solution in Postgres as requested in the comments.
Assuming status to be NOT NULL.
Assuming at least one row with status FALSE and one with status TRUE exist at all times.
WITH cte AS (
(
SELECT id, letters, status
FROM tbl
WHERE status -- 1 row with status true
ORDER BY random()
LIMIT 1
)
UNION ALL
(
SELECT id, letters, status
FROM tbl
WHERE NOT status -- 1 row with status false
ORDER BY random()
LIMIT 1
)
)
SELECT * FROM cte
UNION ALL -- add another random row
(
SELECT id, letters, status
FROM tbl
LEFT JOIN cte c USING (id)
WHERE c.id IS NULL -- don't select row twice
ORDER BY random()
LIMIT 1
)
ORDER BY random(); -- order 3 rows randomly
MySQL does not supports CTEs.
All parentheses are necessary. Details:
PostgreSQL combine multiple select statements
This is not very efficient for big tables. For better performance consider this related answer:
Best way to select random rows PostgreSQL
If status TRUE and FALSE are not extremely unbalanced, I would write a plpgsql function that loops through the randomly sorted table (or selection like in the the linked answer) until I have three rows with at least one of each status. Would be much faster.

how to use join in this case

I have two tables:
table pin_info:
id | member_id | look_week | look_name | is_pinned | date
1 | 1 | 3 | the improviser | yes | 2013-11-19 21:57:04
2 | 1 | 2 | destined for stardom | yes | 2013-11-19 21:56:00
3 | 1 | 1 | fashinably corporate | no | 2013-11-19 21:54:00
table arrow_rating:
id | member_id | look_week | look_name | rating |
1 | 1 | 3 | the improviser | 3 |
2 | 1 | 2 | destined for stardom | 4 |
3 | 2 | 1 | fashinably corporate | 5 |
I want is_pinned(from pin_info) and rating(from rating) .I will be having parameter member_id and look_week. (assume 1 and 2 respectively)
What I have done:
SELECT p_i.is_pinned,a_r.rating
FROM pin_info p_i,arrow_rating a_r
WHERE p_i.look_week=a_r.look_week AND p_i.member_id='1'
I am sure this is not the correct way.Any help?
Try this:
SELECT pin_info.is_pinned, arrow_rating
FROM pin_info INNER JOIN arrow_rating
ON pin_info.look_week = arrow_rating.look_week
WHERE pin_info.id = '1';
SELECT is_pinned, rating
FROM pin_info
LEFT JOIN arrow_rating USING (look_week, member_id)
WHERE pin_info.member_id = 1
AND pin_info.look_week = 2
That will select where both member_ids equal 1, and look_week equals 2
The result set for the above is:
is_pinned | rating
------------------
yes | 4

Count the frequency of votes in MySQL

I am making a website where users can vote on which category a page is. They can vote that the page is in category a, b, c, or d.
I need to find the most commonly occurring category in the MySQL row out of all the votes.
Each time a user submits their vote, it submits the "category" that they voted for, and the "page_id".
I have this so far:
SELECT page_id, category
FROM categories
GROUP BY page_id
I cannot use a COUNT(*) WHERE category = 'a' then repeat it for each category because there is many more categories in the actual project.
If your table looks something like this:
SELECT * from categories;
+---------+----------+
| page_id | category |
+---------+----------+
| 1 | a |
| 1 | b |
| 1 | a |
| 1 | c |
| 1 | a |
| 1 | b |
| 1 | a |
| 2 | d |
| 2 | d |
| 2 | c |
| 2 | d |
| 3 | a |
| 3 | b |
| 3 | c |
| 4 | c |
| 4 | d |
| 4 | c |
+---------+----------+
17 rows in set (0.00 sec)
Then you may want to try this query:
SELECT c1.page_id, MAX(freq.total),
(
SELECT c2.category
FROM categories c2
WHERE c2.page_id = c1.page_id
GROUP BY c2.category
HAVING COUNT(*) = MAX(freq.total)
LIMIT 1
) AS category
FROM categories c1
JOIN (
SELECT page_id, category, count(*) total
FROM categories
GROUP BY page_id, category
) freq ON (freq.page_id = c1.page_id)
GROUP BY c1.page_id;
Which returns this:
+---------+-----------------+----------+
| page_id | MAX(freq.total) | category |
+---------+-----------------+----------+
| 1 | 4 | a |
| 2 | 3 | d |
| 3 | 1 | a |
| 4 | 2 | c |
+---------+-----------------+----------+
4 rows in set (0.00 sec)
Compare the results with the actual frequency distribution:
SELECT page_id, category, COUNT(*) FROM categories GROUP BY page_id, category;
+---------+----------+----------+
| page_id | category | COUNT(*) |
+---------+----------+----------+
| 1 | a | 4 |
| 1 | b | 2 |
| 1 | c | 1 |
| 2 | c | 1 |
| 2 | d | 3 |
| 3 | a | 1 |
| 3 | b | 1 |
| 3 | c | 1 |
| 4 | c | 2 |
| 4 | d | 1 |
+---------+----------+----------+
10 rows in set (0.00 sec)
Note that for page_id = 3, there is no leading frequency, in which case this query makes no guarantee on which category will be chosen in such a case.
something like
SELECT category, page_id, count(vote_id)
FROM categories
WHERE category in ('a', 'b', 'c', 'd')
GROUP BY category, page_id
ORDER BY count(vote_id) DESC
LIMIT 1
should do the trick. I assume here the votes are individually stored in a separate row per vote.
It only looks in the cqtegory you're interested in, sorts with the most votes first and only returns the first one.

Categories