Mysql Select one record from another - mysql

Hello I have huge problem. In mysql I have 3 tables. Clients , Groups and GroupCross . So I have :
SELECT * FROM clients AS cl
INNER JOIN groupscross AS cr ON cl.cid=cr.cid;
(So now I have matched id in clients with id in groupcross . In groupcross I have also table :cgid .
Now I have data from cr.cgid like : 50,50,60,55,60 so when I command :)
WHERE cr.cgid='function to get id' .
I get all cgid:(50,50,60,55,60)
And here is my question:
How can I do to have all id without 60 ?
I add when I write: WHERE cr.cgid <>60 it shows me all record from database when I want only 50,50,55

Use an IN statement -
SELECT *
FROM `clients` AS `cl`
INNER JOIN `groupscross` AS `cr`
ON `cl`.`cid` = `cr`.`cid`
WHERE `cr`.`cgid` IN(50,55)

Related

How to join three table to check the data in sql?

I know how to join two tables to check data, but I not sure how to join the third table. I have tried to find solution in the online, but I can't find solution it can match my problem. Hope someone can guide me how to join the third table. Thanks.
Below is my sql code, this sql code just to join two tables:
SELECT bl.id_book_name as bl_book_name
, bl.remark as bl_remark
, bl.status as status
, bl.return_date as bl_return_date
, anb.country as anb_country
, anb.title as book_name
, bl.date_borrowed as date_borrowed
FROM book_lending bl
JOIN add_new_book anb
ON bl.id_book_name = anb.id
My third table column in the below, table name is called user, I want to join the user name:
id |name |
This is my join two tables testing result:
Since you have not provided the full structure of USER table, I can only provide you the glimpse of joining third table. You may try below code -
SELECT bl.id_book_name as bl_book_name
, bl.remark as bl_remark
, bl.status as status
, bl.return_date as bl_return_date
, anb.country as anb_country
, anb.title as book_name
, bl.date_borrowed as date_borrowed
FROM book_lending bl
JOIN add_new_book anb ON bl.id_book_name = anb.id
JOIN user u ON u.id = <USER_ID column in 1 of above 2 tables>

how select row in MySQL by check when column data include delimiter

I have question . If i have user table and a column name be freinds .
I want use some user id inside a column friends with this value : 1|2|45|18 .
This means for example user id =5 have 4 freinds with these id 1|2|45|18.
I don't want use two table for this situation.
How can select each users data when friends with user id = 5 ?
In php we have explode function for slice each delimeter .
Can i set delimeter for this target or i should use two table user and friends in MySQL ?
It's an awful case, especially if you inherit some DB with such a scheme, but to find friends you can use:
SELECT friend.*
FROM user u
left join user friend on concat('|', o.friends, '|') like concat('%|', friend.id, '|%')
where u.id = 5;

How to copy 3 columns from one table into another in mysql

I have two tables and both include 2 columns, sureness and kindness and and they are related to each other by dataitemID and id. Just to show you the structure I will put 2 selects that I got from database:
SELECT ID,sureness,kindness FROM omid.tweet ;
SELECT ID,DataitemID,sureness,kindness FROM omid.entity_epoch_data ;
and I want to copy all value of sureness and kindness in omid.tweet into entity_epoch_data where entity_epoch_data.entityID is equal to entityID coming from entity_relation where tweet.ID =entity_relation.ID
I want just to do it in mysql rather than reading the whole table in java and updating the database in the loop but I am so confused. How can I do that?I appreciate any help:)
Update:
I wrote the code as follow but it does not work:
update tweet, entity_epoch_data
set entity_epoch_data.sureness= tweet.sureness,
entity_epoch_data.kindness = tweet.kindness ,
entity_epoch_data.calmness = tweet.calmness ,
entity_epoch_data.happiness = tweet.happiness
WHERE entity_epoch_data.EntityID in(
SELECT EntityID FROM omid.entity_dataitem_relation
INNER JOIN omid.tweet t ON entity_dataitem_relation.DataitemID = t.ID)
It's actually pretty straight forward. The UPDATE clause works like a JOIN and then use SET to set the values
UPDATE tweet INNER JOIN entity_epoch_data
ON tweet.id = entity_epoch_data.id
SET entity_epoch_data.sureness= tweet.sureness,
entity_epoch_data.kindness = tweet.kindness

Combining two columns on a id

this is my database
database schema http://slashdir.com/php/blogg/images/bloggdb.png
What i want to do, is, for a given userid, show the total times he has been reported.
I have read various other questions on the matter, but I'm still stumped.
The latest query i tried was
select
sum(posts.timesreported + comments.timesreported) AS total_reports
FROM
posts
INNER JOIN comments ON (posts.userid = comments.userid)
WHERE posts.userid=5 AND comments.userid=5;
But this must be wrong as the number i get is much too high
Thanks!
SELECT
CASE WHEN NULL
THEN 0
ELSE (select sum(posts.timesreported) AS total_posts_reports
FROM posts INNER JOIN users ON (posts.userid = users.id)
WHERE posts.userid=5)
END
+
CASE WHEN NULL
THEN 0
ELSE (select sum(comments.timesreported) AS total_comments_reports
FROM comments INNER JOIN users ON (comments.userid = users.id)
WHERE comments.userid=5)
END
FROM DUAL;
Instead of
sum(posts.timesreported + comments.timesreported) AS total_reports
try
sum(posts.timesreported) + sum(comments.timesreported) AS total_reports
and I think you need to group by userId
WHERE posts.userid=5 AND comments.userid=5; is unnecessary since the tables are joined.
And sum operator is not correct logically
Use this query
select
sum(posts.timesreported) + sum(comments.timesreported) AS total_reports
FROM
posts
INNER JOIN comments ON (posts.userid = comments.userid)
WHERE posts.userid=5
It looks like your collecting the sum PRIOR to singling out the user. Perhaps this is adding those column values for all users prior to the join? What happens if you SELECT *, perform your INNER JOIN where userid = 5. Save the column values as two variables and then try to add them. Do you get the same result?
This might help you error check to see if the above theory is accurate.
<?php
// Connects to your Database
mysql_connect("your.hostaddress.com", "username", "password") or die(mysql_error());
mysql_select_db("Database_Name") or die(mysql_error());
//Run Query
$NUM1=mysql_query("SELECT Field1 FROM Table WHERE user.key=5");
$NUM2=mysql_query("SELECT Field2 FROM Table WHERE user.key=5");
//Print Each Result
echo 'Num1 = '.$NUM1;
echo 'Num2 = '.$NUM2;
//Print Total
$TOTAL = $NUM1 + $NUM2;
echo 'Total = '.$TOTAL;
?>

specific select work only when i add db name

I'm using SQL Server 2008 Express
All of the select statments are fine. Now I have this one:
SELECT
ORG.id, ORG.img, ORG.name, ORG.city, ORG.address,
ORG.zip, ORG.telephone, ORG.telephone2,
ORG.fax, ORG.email, ORG.vaname, ORG.vanumber,
ORG.yor_photo, ORG.commission,
Clients.id AS yor_id, Clients.prefix,
Clients.fname, Clients.lname, Clients.phone,
Clients.pelephone, Clients.email, Clients.pid,
ORG.adddate, ORG.note
FROM Org
LEFT OUTER JOIN
(select * from Clients where yor = 1) as Clients ON Clients.company = ORG.id
WHERE ORG.id=" & ORGID
It is not working, I get error "invalid object name"
If I add the DBNAME.DBO in front of the table name it works
The problem is that i don't want to change that on every project
Why is it not working?
UPDATE
the problem is not with the db name, the problem is with the AS yor_id in the select statment.
if i remove it the record retrieved is not full with all the data but if i write it the data is full but yor_id is empty
UPDATE
NEVER MIND, my bad! id column was corupted
Before that query you need to write
use DBNAME
in order to switch to your dbname.
Probably you are running that query on master database, so just do this and should work:
use DBNAME
SELECT
ORG.id, ORG.img, ORG.name, ORG.city, ORG.address,
ORG.zip, ORG.telephone, ORG.telephone2,
ORG.fax, ORG.email, ORG.vaname, ORG.vanumber,
ORG.yor_photo, ORG.commission,
Clients.id AS yor_id, Clients.prefix,
Clients.fname, Clients.lname, Clients.phone,
Clients.pelephone, Clients.email, Clients.pid,
ORG.adddate, ORG.note
FROM Org
LEFT OUTER JOIN
(select * from Clients where yor = 1) as Clients ON Clients.company = ORG.id
WHERE ORG.id=" & ORGID