sql statement to select all from another table [closed] - mysql

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!

Related

SQL to find mutual friends from the same 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 2 years ago.
Improve this question
I am trying to find the mutual friends with SQL from a single table
I have a simple table with 2 fields user, friends as int. Data values as are as below
Expected Output
user friend mutual friends
1 2 2
1 3 1
This is the query i tried
select ex1.user,ex2.friend, count(distinct ex3.friend) from (select distinct user from exer1) ex1
join exer1 ex2, exer1 ex3
where ex1.user=ex2.user and
ex2.user<>ex3.user and ex2.friend<>ex3.friend
group by ex1.user,ex2.friend order by 1,2
The output i got was
enter image description here
The desired output I am looking for is
enter image description here
You can use a self-join and aggregation:
select f1.user, f2.user, count(*)
from friends f1 join
friends f2
on f1.friend = f2.friend and
f1.user < f2.user
group by f1.user, f2.user;

MYSQL SUM Value [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 8 years ago.
Improve this question
I have 2 table :
table structure for "kegiatanrenja"
idkegiatan tahun koderekening detail
renja001 2001 1.03.03.04.1 A
renja002 2001 1.03.03.03.2 B
renja003 2001 1.08.08.05.3 C
renja004 2001 1.08.08.05.2 D
table structure for "sumberdana"
idkegiatan nilai kodesumberdana
renja001 100 1
renja002 200 2
renja003 50 1
renja004 100 1
I want to create a query that displays the value of all the columns "nilai" if MID (koderekening, 9,2) the same value, along with the value of another column, where each row in the group by where each row in the group by MID (koderekening, 9,2).
kode rekening nilai_group(SUM Of value the same kode MID(koderekening,9,2)
1.03.03.04 300
1.08.08.05 150
please help, every suggestion will be very appreciated.
SELECT LEFT(koderekeneing,0,10), sum (nilal)
FROM kegiatanrenja k
JOIN sumberdana s ON s.idkegiatan = k.idkegiatan
GROUP BY MID(koderekening,9,2)
UNION
SELECT 'ALL', sum (nilal)
FROM kegiatanrenja k
JOIN sumberdana s ON s.idkegiatan = k.idkegiatan
Please note, this is VERY easy, so if you are asking this sort of question on stack lot, you are going to receive a negative response. Please get yourself a good SQL book and do some studying.

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.

Executing "simple" sql query [closed]

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#