I want to get all record data as single column using SQL query.User Table
user_id username parent_id
10 user1 5
12 user2 3
14 user3 2
.. .. ..
get all users with parent id as single column
Need results as below (get data from user_id & parent_id)
users OR users
10 10
12 5
14 12
5 3
3 14
2 2
.. ..
Here just need list of user sequence is not important.Is this possible in sql query? is there any SQL function for that?
This will do the trick.
SELECT user_id AS users FROM Users
UNION
SELECT parent_id AS users FROM Users
Use UNION ALL if you need duplicate values (such as in the parent_id column) to return as individual rows. Make sure the alias is the same for both halves to get it to return in a single column. This will return unordered, so rerunning the query might not always give the same result (meaning same data in same order), but it will always give the same data.
Related
select * from "Test"."EMP"
id
1
2
3
4
5
Select SUM(1) FROM "Test"."EMP";
Select SUM(2) FROM "Test"."EMP";
Select SUM(3) FROM "Test"."EMP";
why the output of these queries is?
5
10
15
And
I don't understand why they write table name like this "Test"."EMP"
your table has 5 records. the statement select 1 from test.emp returns 5 records with values as 1 for all 5 records.
id
1
1
1
1
1
This is because db engine simply returns 1 for each existing record without reading the contents of the cell. and same happens for select <any static value> from test.emp
same happens for 2 and 3
id
2
2
2
2
2
hence there are 5 records returned with the static values and sum of those values will be the product of static number passed in the select statement and total records in the table
additional fact: It is always recommended to perform count(1) than count(*) as it consumes less resource and hence less load on the server
I don't think it's "Test"."EMP" with double quotes.. it's probably `Test`.`EMP` with backticks instead. The definition means its database_name.table_name. This is the recommended format to get the correct table_name from database_name; in this case, you're specifically making the syntax to query from `Test`.`EMP`. Read more about identifier qualifiers.
As for SUM(x), the x get's repeated according to the rows present in the table. So SUM(1) on 5 rows is 1+1+1+1+1, SUM(2) on 5 rows is 2+2+2+2+2, and so on.
SELECT id,name,info FROM table LIMIT 5
the result Set should be contains 5 rows wich is The first 5 rows of the table,but is any exception about this usage? .the table like this :
SELECT * FROM table limit 10;
1. company_id company_name tel
1 TCL集团股份有限公司 0752-2288333
2 UNITEDSTACK(北京)科技有限公司 15727325616
3 《市政技术》杂志社有限公司 13401070358
4 《网络安全技术与应用》杂志社有限公司 010-62765013
5 《艺术市场》杂志社股份有限公司 64271947
7 一呼医知己健康咨询(北京)有限公司 010-62957992
8 一呼(北京)电子商务有限公司 62957992
9 一汽轿车股份有限公司 0431-85782608
10 一通万通商务服务(北京)有限公司 010-68061805
I use the first sql the result is normal:like this
SELECT company_id,company_name,tel FROM table LIMIT 5;
1. 1 TCL集团股份有限公司 0752-2288333
2 UNITEDSTACK(北京)科技有限公司 15727325616
3 《市政技术》杂志社有限公司 13401070358
4 《网络安全技术与应用》杂志社有限公司 010-62765013
5 《艺术市场》杂志社股份有限公司 64271947
However I use the second sql like this :
SELECT comapny_id,company_name FROM table LIMIT 5;
1275992
1758051
2990914
5241776
5344925
We are seeing the result is not the 5 rows of the table obviously,the difference of these fileds is that company_id is a primary key,company_name is a type of MUL.can you help me?thank you very much!
the result Set should be contains 5 rows wich is The first 5 rows of
the table,but is any exception about this usage?
Only the first part of this statement is correct. Your query returns 5 rows from the query. However, those are 5 indeterminate rows.
SQL tables represent unordered sets. Hence, there is no first five rows in a table. If you want your result set ordered, then you need to include an order by clause. Often, an auto-incremented id is used for this purpose, because such an id captures the order that rows are inserted into the table.
i have table lets say - Students,
with 5 records and id(s) are 1 to 5, now i want to select the records - in a way that result should come like given sorting order of id column
id column should be resulted - 5,2,1,3,4
is there any other way to do this - then separate db calls for ids?
single db call ?
I guess if you really want a hard-coded order, you could do something like this:
order by case id
when 5 then 0
when 2 then 1
when 1 then 2
when 3 then 3
when 4 then 4
else 999
end
Or more simply (as #Strawberry points out in the comments):
order BY FIELD(id,4,3,1,2,5) desc
I am trying to query a dataset from a single table, which contains quiz answers/entries from multiple users. I want to pull out the highest scoring entry from each individual user.
My data looks like the following:
ID TP_ID quiz_id name num_questions correct incorrect percent created_at
1 10154312970149546 1 Joe 3 2 1 67 2015-09-20 22:47:10
2 10154312970149546 1 Joe 3 3 0 100 2015-09-21 20:15:20
3 125564674465289 1 Test User 3 1 2 33 2015-09-23 08:07:18
4 10153627558393996 1 Bob 3 3 0 100 2015-09-23 11:27:02
My query looks like the following:
SELECT * FROM `entries`
WHERE `TP_ID` IN('10153627558393996', '10154312970149546')
GROUP BY `TP_ID`
ORDER BY `correct` DESC
In my mind, what that should do is get the two users from the IN clause, order them by the number of correct answers and then group them together, so I should be left with the 2 highest scores from those two users.
In reality it's giving me two results, but the one from Joe gives me the lower of the two values (2), with Bob first with a score of 3. Swapping to ASC ordering keeps the scores the same but places Joe first.
So, how could I achieve what I need?
You're after the groupwise maximum, which can be obtained by joining the grouped results back to the table:
SELECT * FROM entries NATURAL JOIN (
SELECT TP_ID, MAX(correct) correct
FROM entries
WHERE TP_ID IN ('10153627558393996', '10154312970149546')
GROUP BY TP_ID
) t
Of course, if a user has multiple records with the maximal score, it will return all of them; should you only want some subset, you'll need to express the logic for determining which.
MySql is quite lax when it comes to group-by-clauses - but as a rule of thumb you should try to follow the rule that other DBMSs enforce:
In a group-by-query each column should either be part of the group-by-clause or contain a column-function.
For your query I would suggest:
SELECT `TP_ID`,`name`,max(`correct`) FROM `entries`
WHERE `TP_ID` IN('10153627558393996', '10154312970149546')
GROUP BY `TP_ID`,`name`
Since your table seems quite denormalized the group by name-par could be omitted, but it might be necessary in other cases.
ORDER BY is only used to specify in which order the results are returned but does nothing about what results are returned - so you need to apply the max()-function to get the highest number of right answers.
I have a query that fetches the list of user IDs and their corresponding user names on a board but from another table also gets a column that has a value (a name) on the row corresponding to the user ID if said user has changed their name. Using an outer join I got the three nicely displayed as in the following example of a few of the results:
member_id name dname_current
1 Blablabla1 blablabla2
2 Bla4444
3 RevZ
5 Herpaderp42
6 Lalalala
7 Kaboom
14 testtesttest21 Formula21
15 Alex Ethan
16 Bob Radio3
The SQL query to get the three columns is as follows:
SELECT
data_members.member_id,
data_members.name,
data_dnames_change.dname_current
FROM data_members LEFT OUTER JOIN data_dnames_change
ON data_members.member_id = data_dnames_change.dname_member_id
GROUP BY data_members.member_id
Is there a way to display this so that it merges the values which exist in the 'dname_current' column of that other table into the 'name' column, replacing any value that's already in the corresponding row of that column?
COALESCE() returns the first non-null value, so you can do the following to prefer dbname_current over data_members.name unless it is NULL:
SELECT
data_members.member_id,
COALESCE(data_dnames_change.dname_current, data_members.name) AS name
FROM data_members LEFT OUTER JOIN data_dnames_change
ON data_members.member_id = data_dnames_change.dname_member_id
GROUP BY data_members.member_id
Should return:
member_id name
1 blablabla2
2 Bla4444
3 RevZ
5 Herpaderp42
6 Lalalala
7 Kaboom
14 Formula21
15 Ethan
16 Radio3