Combine Multiple Query with one standart column - mysql

I using MySQL, I have 2 Query Select SQL, first Query will return Result A, and second will return Result B, so i want to combine that two Query Result, ID column of Result A and value_num of Result B become standard Column, and i don't think for use subselect because i have tried that and it will take long time effort in query process , how can i do this ?
Result A :
+------------+--------------+
+ ID + Name +
+------------+--------------+
+ 1 + Steve +
+ 2 + Mile +
+ 3 + Santo +
+ 4 + Del Piero +
+ 5 + Jack +
============================+
Result B :
+------------+--------------+
+ Valuenum + value +
+------------+--------------+
+ 1 + 20 +
+ 2 + 30 +
+ 6 + 44 +
+ 7 + 55 +
============================+
Combine Result A dan Result B, below is my expect output.
+------------+--------------+----------+
+ID_valuenum + Name + value +
+------------+--------------+----------+
+ 1 + Steve + 20 +
+ 2 + Mile + 30 +
+ 3 + Santo + Null +
+ 4 + Del Piero + Null +
+ 5 + Jack + Null +
+ 6 + Null + 44 +
+ 7 + Null + 55 +
============================+==========+
Thanks

select
ta.Id as ID_Valuenume
,ta.name
,tb.value
from TableA ta
left join TableB tb on ta.ID=tb.valuenum
union
select
tb.valuenum
,ta.name
,tb.value
from TableB tb
left join TableA ta on ta.ID=tb.valuenum

You have to use UNION for this :
select ra.ID as ID_valuenum,
ra.Name,rb.value from ResultA ra left join ResultB rb on ra.ID=rb.Valuenum
UNION
select rb.Valuenum,null as Name,rb.value from ResultB where rb.Valuenum not in
(select ID from ResultB)
->In first part you have to use Left Join so it will give you data
+------------+--------------+----------+
+ID_valuenum + Name + value +
+------------+--------------+----------+
+ 1 + Steve + 20 +
+ 2 + Mile + 30 +
+ 3 + Santo + Null +
+ 4 + Del Piero + Null +
+ 5 + Jack + Null +
->In second part its not required to use any join then it will give data:-
+ 6 + Null + 44 +
+ 7 + Null + 55 +

Related

How to order the table data with double condition order in mysql

I have table like this :
+-------------+------------+
+ my_id + name +
+-------------+------------+
+ 1 + Adam +
+-------------+------------+
+ 2 + Udin +
+-------------+------------+
+ 3 + Asep +
+-------------+------------+
+ 4 + Jarwo +
+-------------+------------+
+ 5 + Bambang +
+-------------+------------+
And then I wanna show just my data have a newest id and limit my data until 3 row, like this :
+-------------+------------+
+ my_id + name +
+-------------+------------+
+ 5 + Bambang +
+-------------+------------+
+ 4 + Jarwo +
+-------------+------------+
+ 3 + Asep +
+-------------+------------+
For the problem is I wanna order again my data into ascending by name field, and when I insert a query for order by after sorting my id by descending is not working and my data is not sorting by name, for my query code like this :
SELECT tables.my_id, tables.name
FROM tables
ORDER BY tables.my_id DESC, tables.name ASC
LIMIT 3
And for the result still same with my code when I sorting my id by descending, and my expectation like this :
+-------------+------------+
+ my_id + name +
+-------------+------------+
+ 3 + Asep +
+-------------+------------+
+ 5 + Bambang +
+-------------+------------+
+ 4 + Jarwo +
+-------------+------------+
so what's wrong with my code? Thanks before
The alias "tables" is not defined
tables.my_id + 0; Does nothing
What I understand ID is unique; So second sort will not work
SELECT * FROM (
SELECT my_id, tables.name
FROM occupants
ORDER BY my_id DESC
LIMIT 3
) t
ORDER BY t.name ASC

MySQL: Get top 30 rows with the highest number of columns set

I've got a table with the following schema:
user_id | A | B | C | D | E | F | G | H | I | J | K | L | M
------------------------------------------------------------
3829 | 1 | 0 | 0 | 1 | 1 | 1 | 0 | 1 | 0 | 1 | 0 | 1 | 1
The colums A-M have boolean values, which are obviously 0 or 1.
Is there a way, aside from looping through all the columns, to:
get the number of columns that are set to 1 for a specific user, as well as
get the top 30 (or n) users who have the most columns set to 1?
I have a fair amount of experience with php and MySQL but a query like this is puzzling me.
I'm envisioning something of this sort:
$statement = "SELECT * FROM my_table WHERE count(*) > 1 ORDER BY DESC LIMIT 30";
Any help would be great :)
you can also use plus(+) as sum
SELECT A + B + C + D + F + G + H + I + J + K + L + M AS total,user_id
FROM my_table
ORDER BY total
LIMIT 30
This could be accomplished using adding the booleans and a FROM sub-query select statement.
As bool is stored as a bit you can use sum to get a running total for each row.
With this data then get to top n results with the highest value.
Something like this:
SELECT * FROM (
SELECT *, A + B + C + D + E + F + G + H + I + J + K + L + M AS Total
FROM Table1
)
ORDER BY Total DESC
LIMIT 30;
Or if you do not want to use a sub-query you will have to use a group by due to the use of the aggregate SUM function.
UPDATE: As I am not using the SUM function any more the group by in the second function is not needed so I have removed that too.
Something like this:
SELECT *, A + B + C + D + E + F + G + H + I + J + K + L + M AS Total
FROM Table1
ORDER BY Total DESC
LIMIT 30;
The logic is simple you just have to sort the data in decreasing order of the sum of all the other numeric column and fetch the first 30. That will give you the Top 30. This is tricky, as there is no aggregation to be done, so using sum function here is not right. So your query is supposed to be:
SELECT *
FROM Table1
ORDER BY (A + B + C + D + F + G + H + I + J + K + L + M) DESC
LIMIT 30;
Here is an SQL Fiddle Demo

Translation Inner Join in JQL

I have a problem with INNER JOIN in my JQL. What I want to do is to retrieve all Users that have rate one User Z with a mean of >= 2. Users have to answer 3 questions that will determine the final rate of this User Z. There is 2 tables, User and Score.
Table User Table Score
Id_user Id_score Id_rated Id_rater Id_question Score
1 1 1 3 1 1
2 2 1 3 2 0
3 3 1 3 3 1
4 1 2 1 0
5 1 2 2 0
6 1 2 3 0
I want as a result only User 2 for example.
This is the error when I translare my query to JQL :
[32, 184] The join association path is not a valid expression.
My query works well in MySQL Workbench
SELECT *
FROM User
INNER JOIN (
SELECT ID_RATER, (AVG(Score.score)*5) as AvgScore
FROM Score
WHERE ID_RATED=1751
AND (
SELECT (AVG(Score.score) * 3)
FROM Score)
GROUP BY ID_RATER
) TabAvg
ON TabAvg.AvgScore >= 2
AND USER.ID=TabAvg.ID_RATER
Translating to JQL :
String sql =
"SELECT U "
+ "FROM User U "
+ "INNER JOIN "
+ "("
+ "SELECT S.id_rater, (AVG(S.Score) * 3) AS AvgScore "
+ "FROM Score S "
+ "WHERE S.id_rated= :id_rated"
+ "AND "
+ "("
+ "SELECT (AVG(S2.score) * 3) "
+ "FROM Score S2"
+ ") "
+ "GROUP BY S.id_rater"
+ ") TabAvg "
+ "ON TabAvg.AvgScore >= 2 "
+ "AND U.id_user = TabAvg.id_rater";
Well, I still don't know what the problem with
EntityManager.createQuery(sql)
but to bypass this, I used
EntityManager.createNativeQuery(sql)
by just copy/paste my working sql into my code.

How to count sum of three columns MySql

I have three columns with names:
projectNo| process | procLeader | procCheker | Stuff |
----------+---------+------------+-------------+---------------+
16090001 | ANM | ben | barry | bob, bart, bok|
16090001 | BLD | anton | kirill | kart, ali |
What I want to is to count procLeader, procChecker, stuff columns assigned to projectNo. I managed to count each column by using query:
SELECT
COUNT(procLeader) AS `ld`,
COUNT(procChecker) AS `ch`,
SUM((LENGTH(stuff) - LENGTH(REPLACE(stuff,",","")) + 1)) AS `st`
FROM `process`
WHERE projectNo=16090001;
I get
ld| ch | st |
---+----+----+
2| 2| 5|
I need something like 'total' table
How I can sum this values? or maybe use another method?
SELECT
COUNT(procLeader) AS `ld`, COUNT(procCheker) AS `ch`,
SUM((LENGTH(stuff) - LENGTH(REPLACE(stuff,",","")) + 1)) AS `st` ,
(
COUNT(procLeader) +
COUNT(procCheker) +
SUM((LENGTH(stuff) - LENGTH(REPLACE(stuff,",","")) + 1))
) As `Total`
FROM `process` WHERE projectNo=16090001
Please let us know if you have any concerns or que.
Use the following query and it worked:
SELECT
COUNT(procLeader) AS `ld`, COUNT(procChecker) AS `ch`,
SUM((LENGTH(stuff) - LENGTH(REPLACE(stuff,",","")) + 1)) AS `st` ,
(
COUNT(procLeader) +
COUNT(procChecker) +
SUM((LENGTH(stuff) - LENGTH(REPLACE(stuff,",","")) + 1))
) As `Total`
FROM `process` WHERE projectNo = 16090001
Output:
Id - ch - st - Total
2 - 2 - 5 - 9
not sure I understand question, but if you want to count characters, did you try char_length and concat ?
SELECT char_length(concat(procleader, proccheker, REPLACE(stuff,',',''))) FROM process WHERE projectNo=16090001

MySQL select user who less performed an action

I have the following table structures:
ACTIONS
+ userid + action +
+----------+----------+
USERS
+ id + name +
+----------+----------+
+ 1 + james +
+----------+----------+
+ 2 + john +
+----------+----------+
A data example:
ACTIONS
+ userid + action +
+----------+----------+
+ null + action1 +
+----------+----------+
+ 1 + action2 +
+----------+----------+
+ 1 + action3 +
+----------+----------+
+ 2 + action4 +
+----------+----------+
I need a SELECT query that will return the user who performed less actions.
If all fields are null (the first launch) or all equal (all users performed the same action), it can return 1 user (rand, asc, desc, it's the same).
///////////////////
EDIT
Based on the Richard Hamilton's reply, this query only works with users already in ACTIONS table. If one or more user_id are NULL or users are not in ACTIONS, doesn't select from USERS table
SELECT id FROM users
INNER JOIN actions ON users.id = actions.user_id
GROUP BY user_id
ORDER BY COUNT(user_id)
LIMIT 1;
You'll want to use GROUP BY. We can then ORDER BY the number of actions they performed.
To find the one with the lowest number, just use LIMIT 1
SELECT user_id FROM users
INNER JOIN data ON users.id = data.users_id
GROUP BY action
ORDER BY COUNT(action)
LIMIT 1;