Executing "simple" sql query [closed] - mysql

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 9 years ago.
Improve this question
Here is data in my table :
id name surname place
1 test1 isdad YES
2 test2 spreda YES
3 test3 me NO
4 test4 smallvile YES
What I'd like to get, all ids where place='YES'. So Ids are
1
2
4
and the result count is 3. I want to append that number to the end of the results, so the result should look like this:
1
2
4
3

There is almost definitely a better way to do this. That said, you could do:
SELECT id FROM myTable WHERE place = 'YES'
UNION ALL
SELECT COUNT(*) FROM myTable WHERE place = 'YES'
UNION ALL will append the result number as a new row rather than attempting to add it as another column.
Please don't do this; the results will be mixed, it stands in stalwart defiance of good software design and will be baffling to anyone who comes along later and has to work on this piece of code.

In MySQL, you can do something similar to this in a single query using WITH ROLLUP:
select id, count(*)
from myTable
where place='YES'
group by id
with rollup

SELECT id FROM TABLE WHERE place='YES'
will return with the ids what you want, and the rowcount will store the count number, if you use this with php or c++/c#

Related

how to select sum of values by id's? SQL [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
So, i have a table with records like this:
ID ORDERS
1 4
1 1
2 4
3 1
3 2
I need to create a query that will lead to this situation:
ID ORDERS
1 5
2 4
I tried with select id, count(orders) from table group by id having count(orders) > 3
but all I get is a strange result in which my ids are strangely numbered.
You need to sum and not count:
select id, sum(orders)
from table
group by id
having sum(orders) > 3
MySQL supports summing up values via sum like follows:
select id, sum(orders) from test group by id;
See also DB Fiddle.
Change your attempt so that you SUM(orders) not COUNT(orders)
Count doesn't work because it increments a counter by one for any non null value, and doesn't increment for a null value. In the the following table:
ID, name
1, John
2, null
3, Jane
null, null
COUNT(id) will return 3 - there are 3 non null ID values. COUNT(name) will return 2 - there are two non null values. SUM(ID) will return 6 - the sum of the numeric values of ID is 6. SUM looks at what the values are, COUNT doesn't. SUM will treat nulls as 0. The special case COUNT(*) will count the presence of the row even if all its values are null. COUNT(*) will return 4 - there are 4 rows in the results

sql statement to select all from another table [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
lists table
id user_id list_name
1 1 test
2 1 test2
items table
id list_id item_name price_item item_checked
1 1 apple 2 0
2 2 orange 2 0
result should be
id item_name price_item item_checked
1 Apple 2 0
How would i accomplish to select all from list 1
Your question is not clear but I will try my best to answer it.
To get the results you have posted:
select *
from Items_Table
Title says select all from a different table. I assume lists is the first table and items is the "different" table you are referring to.
Select Items.*
from Lists_Table Lists
left join Items_Table Items on Items.list_id = Lists.id
Now, lets assume you were looking for specific items, say where Id is 1 in the lists table. Then you would have the following:
Select Items.*
from Lists_Table Lists
left join Items_Table Items on Items.list_id = Lists.id
where Lists.id = 1
You can replace Lists.id in the where statement with any other column and set it equal to value you are looking for.
I hope this was helpful. Please try to be a bit more clear about what you are looking for!
Good Luck!

SQL Query group shuffle [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 8 years ago.
Improve this question
I have a query like
select id, item, producer from table
The result is like this:
id item producer
1 apple A
2 pear A
3 peach A
4 orange B
5 strawberry B
6 melon B
I want to shuffle this result (order by id DESC) and get something like this
item producer
strawberry B
pear A
orange B
apple A
peach A
melon B
I DON'T want to display like this:
ALL A ITEM
ALL B ITEM
ALL C ITEM...
Use the rand function in ORDER BY like this:
select id, item, producer from table order by rand();
To Shuffle the selection you can use rand()
The Answer for the link below contains more information.
SELECT id, item, producer
FROM table
ORDER BY RAND()
MySQL: Shuffle a limited query result?
select id, item, producer from table order by rand()
Use Order BY rand() to randomly order the results

mysql query of 3 tables [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 8 years ago.
Improve this question
I have 3 tables as follow:
table "first"
---------------
id item
1 pen
2 book
table "second"
------------------
id color
1 A
2 B
3 C
table "third"
------------------------
id first second
1 1 2
2 2 2
3 2 3
Table "third" has relationship between the "second" and "first"
I would like to query from "first" table for the "item" book which has ID=2 an check it in "third" table and get third.second column value that matches third.first=2 and then pull that values(which could be multiple) and get second.color values from the table "second"
I hope have clearly stated my question.
I have tried this but it gives error:
#1242 - Subquery returns more than 1 row
SELECT color FROM `second` WHERE `id`=
(SELECT second FROM `third` WHERE `first`=
(SELECT id FROM `first` WHERE `item`='book'))
You need JOIN syntax, like this:
SELECT
second.color
FROM
first
LEFT JOIN third
ON first.ID=third.first
LEFT JOIN second
ON third.second=second.ID
WHERE
first.item='book'
AND
second.ID IS NOT NULL
for better understanding JOIN, read this article.

Count number of occurrences by Name [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
First timer here so please excuse me if my question is somewhat confusing. I am attempting to display things in a dynamic table using data pulled from a SQL table and I am having difficulty figuring out the logic.
The table structure looks something like this
NAME Homeruns Hits Bunts Total
Jeff 0 3 1 4
Sally 2 4 0 6
John 3 7 0 10
The data in the table is structured in a way that the type of play being made (Home run, hit, bunt, etc) is in a single column. I'll call this column PLAY. The name is in a separate column. This column will be called NAME. Table name is BASEBALL.
This is called a pivoting query. You can do this in standard SQL with aggregation:
select name,
sum(case when play = 'HomeRun' then 1 else 0 end) as HomeRun,
sum(case when play = 'hit' then 1 else 0 end) as Hit,
sum(case when play = 'bunt' then 1 else 0 end) as Bunt,
count(*) as Total
from baseball bb
group by name;
This is a pretty simple SQL query, so I'm guessing your expertise is less on the database side than on the programming side. I would suggest that you take the time to learn the SQL language properly.