I have two mySql queries to get result from databases. I am trying to join them together.
Query 1:
SELECT userEwallets.id as ewalletId, users.id as userId , money_repositories.money as money, a.nestedUserId
FROM userEwallets
JOIN users ON users.id = userEwallets.userId
JOIN money_repositories ON userEwallets.id = money_repositories.ewalletId
WHERE ewalletNumber = 'SHIRR937303656'
Query 2:
SELECT nested.id as nestedUserId
FROM userEwallets as nested
JOIN users ON users.id = nested.userId
JOIN money_repositories ON nested.id = money_repositories.ewalletId
WHERE ewalletNumber = 'SHIRR9122331743'
and then my combination command:
SELECT userEwallets.id as ewalletId, users.id as userId , money_repositories.money as money, a.nestedUserId
FROM (
SELECT nested.id as nestedUserId
FROM userEwallets as nested
JOIN users ON users.id = nested.userId
JOIN money_repositories ON nested.id = money_repositories.ewalletId
WHERE ewalletNumber = 'SHIRR912233'
) as a
JOIN users ON users.id = userEwallets.userId
JOIN money_repositories ON userEwallets.id = money_repositories.ewalletId
WHERE ewalletNumber = 'SHIRR93730'
I get this error:
#1054 - Unknown column 'userEwallets.id' in 'field list'
Both of commands are same but they have simple difference as ewalletNumber in where clause
UPDATE WITH DATABASE STRUCTURE
money_repositories table:
CREATE TABLE IF NOT EXISTS `money_repositories` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`userId` int(11) NOT NULL,
`ewalletId` int(11) NOT NULL,
`money` int(11) NOT NULL,
`createdAt` int(11) NOT NULL,
`updatedAt` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_persian_ci AUTO_INCREMENT=4 ;
userEwallets table:
CREATE TABLE IF NOT EXISTS `userEwallets` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`userId` int(11) NOT NULL,
`ewalletNumber` varchar(15) COLLATE utf8_persian_ci NOT NULL,
`currencySymbol` varchar(5) COLLATE utf8_persian_ci NOT NULL,
`createdAt` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`updatedAt` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_persian_ci AUTO_INCREMENT=7 ;
users table:
CREATE TABLE IF NOT EXISTS `users` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(20) COLLATE utf8_persian_ci NOT NULL,
`password` varchar(65) COLLATE utf8_persian_ci NOT NULL,
`name` varchar(20) COLLATE utf8_persian_ci NOT NULL,
`family` varchar(20) COLLATE utf8_persian_ci NOT NULL,
`birthDay` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00' ON UPDATE CURRENT_TIMESTAMP,
`email` varchar(20) COLLATE utf8_persian_ci NOT NULL,
`mobileNumber` varchar(15) COLLATE utf8_persian_ci NOT NULL,
`verifyCode` varchar(5) COLLATE utf8_persian_ci NOT NULL,
`photoUri` varchar(50) COLLATE utf8_persian_ci NOT NULL,
`ebanNumber` varchar(20) COLLATE utf8_persian_ci NOT NULL,
`status` tinyint(1) NOT NULL,
`createdAt` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`updatedAt` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_persian_ci AUTO_INCREMENT=35 ;
userEwallets.id should be a.id in Select of your combination command. Because you are getting subquery as a.
Modified combined query;
SELECT a.id AS ewalletId,
users.id AS userId,
money_repositories.money AS money,
a.nesteduserid
FROM (SELECT nested.id AS nestedUserId,
nested.id,
nested.ewalletnumber
FROM userewallets AS nested
JOIN users
ON users.id = nested.userid
JOIN money_repositories
ON nested.id = money_repositories.ewalletid
WHERE nested.ewalletnumber = 'SHIRR912233') AS a
JOIN users
ON users.id = userewallets.userid
JOIN money_repositories
ON userewallets.id = money_repositories.ewalletid
WHERE a.ewalletnumber = 'SHIRR93730'
camleCase query fixed:
SELECT a.id AS ewalletId,
users.id AS userId,
money_repositories.money AS money,
a.nestedUserId
FROM (SELECT nested.id AS nestedUserId,
nested.id,
nested.ewalletNumber
FROM userEwallets AS nested
JOIN users ON users.id = nested.userId
JOIN money_repositories ON nested.id = money_repositories.ewalletId
WHERE nested.ewalletNumber = 'SHIRR912233') AS a
JOIN users ON users.id = nested.userId
JOIN money_repositories ON userEwallets.id = money_repositories.ewalletId
WHERE a.ewalletNumber = 'SHIRR937303'
Because you are not including table userEwallets in the query except in the sub query which won't be recognized outside the subquery. Try
UPDATE
Select
users.id as userId, userEwallets.id As ewalletId,money_repositories.money
From
users
Inner Join
userEwallets ON userEwallets.userId= users.id
Inner Join
money_repositories ON money_repositories.userId=users.id
And
money_repositories.ewalletId = userEwallets.id
Where
userEwallets.ewalletNumber = 'SHIRR93730'
Related
I have the following JOIN I am attempting to create.
SELECT rating_draft_result.COUNT(id), rating_draft_result.user_id, rating_draft_result.result
FROM rating_draft_result
INNER JOIN users ON
rating_draft_result.user_id, rating_draft_result.result = users.id
I am trying to COUNT the total id's of the rating_draft_result table and then also SELECT the user_id and result from that table. Then I am trying to match the user_id and result from the rating_draft_result table to the user's id column. Then I want to get the username from the user's table where the id matches, but I can't figure that part out and I am getting an error anyways with the following code.
I get the following error..
Survey SELECT total count prepare() failed: You have an error in your
SQL syntax; check the manual that corresponds to your MySQL server
version for the right syntax to use near '.id' at line 4
My database tables look like this...
users
CREATE TABLE `users` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`firstname` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
`lastname` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
`email` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
`phone_number` varchar(15) COLLATE utf8_unicode_ci NOT NULL,
`username` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
`password` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
`salt` varchar(32) COLLATE utf8_unicode_ci NOT NULL,
`joined` datetime NOT NULL,
`group` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=105 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
rating_draft_result
CREATE TABLE `rating_draft_result` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` int(11) NOT NULL,
`result` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=28 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
What am I doing wrong in my query?
DESIRED OUTPUT:
users
CREATE TABLE users (
id``firstname``lastname``email``phone_number``username``password``salt``joined group
1 Jack Johnson jack#email.com 2222 jackusername ffd fdddfd today 1
10 Tom Thompson fdfdfddf#fef.com 5555 Tomusername
rating_draft_result
`id` `user_id` `result`
20 1 10
I want to be able to match the rating_draft_result's user_id and result with the user table's id field.
So I want to get the jackusername and tomusername fields.
Check JOIN condition
SELECT rating_draft_result.user_id, rating_draft_result.result, COUNT(*)
FROM rating_draft_result
INNER JOIN users
ON rating_draft_result.user_id = users.id
GROUP BY rating_draft_result.user_id, rating_draft_result.result;
You have
ON rating_draft_result.user_id, rating_draft_result.result = users.id
The problem is in your JOIN ON clause. You need to change it like
INNER JOIN users ON
rating_draft_result.user_id = users.id
(OR) If you want to have both the conditions then use a AND operator like
FROM rating_draft_result
INNER JOIN users ON
rating_draft_result.user_id = users.id
AND rating_draft_result.result = users.id
Change your entire query to be like below
SELECT COUNT(rating_draft_result.id),
rating_draft_result.user_id,
rating_draft_result.result
FROM rating_draft_result
INNER JOIN users ON
rating_draft_result.user_id = users.id
AND rating_draft_result.result = users.id;
I have a photo contest app where users can vote. I would like to select all of the contests where the logged in user has not voted yet.
So I have two tables.
The "contest" table :
CREATE TABLE `contest` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` int(11) NOT NULL,
`category_id` int(11) NOT NULL,
`title` varchar(255) NOT NULL,
`desc` text NOT NULL,
`created_date` datetime NOT NULL,
`started_date` datetime NOT NULL,
`nb_user_min` int(11) NOT NULL,
`nb_photo_max` int(11) NOT NULL,
`nb_photo_per_user` int(11) NOT NULL,
`duration` int(11) DEFAULT NULL,
`status` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
The "contest_vote" table :
CREATE TABLE `contest_vote` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`pic_id` int(11) DEFAULT NULL,
`contest_id` int(11) DEFAULT NULL,
`user_id` int(11) DEFAULT NULL,
`date` datetime DEFAULT NULL,
`ip` varchar(100) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
So to be clear, I want to get the number (or the list) of contests where the user has not voted yet. So I have tried with a LEFT JOIN but it doesn't return the good set of result. Here it the query I have until now :
SELECT DISTINCT c.id, c.title, cv.user_id
FROM contest c
LEFT JOIN contest_vote cv
ON cv.contest_id = c.id AND cv.user_id != ?
GROUP BY contest_id
("?" represents the user_id parameter).
Can you help me to solve this?
This is pretty simple do with a subquery. Just grab all contest without where user is voted like this:
SELECT DISTINCT c.id, c.title
FROM contest c
WHERE c.id NOT IN (SELECT DISTINCT cv.contest_id FROM contest_vote cv WHERE cv.user_id = ?)
Try this one :
SELECT DISTINCT c.id, c.title, cv.user_id
FROM contest c
LEFT JOIN contest_vote cv ON cv.contest_id = c.id AND cv.user_id != ? WHERE c.user_id = ?
GROUP BY contest_id
Help to merge the two sql query (TECDOC).
Display the name and part number;
Display the details of technical parameters.
How to combine these two queries?
SELECT
ART_ARTICLE_NR,
SUP_BRAND,
DES_TEXTS.TEX_TEXT AS ART_COMPLETE_DES_TEXT,
DES_TEXTS2.TEX_TEXT AS ART_STATUS_TEXT
FROM ARTICLES
INNER JOIN DESIGNATIONS ON DESIGNATIONS.DES_ID = ART_COMPLETE_DES_ID
INNER JOIN DES_TEXTS ON DES_TEXTS.TEX_ID = DESIGNATIONS.DES_TEX_ID
INNER JOIN SUPPLIERS ON SUP_ID = ART_SUP_ID
INNER JOIN ART_COUNTRY_SPECIFICS ON ACS_ART_ID = ART_ID
INNER JOIN DESIGNATIONS
AS DESIGNATIONS2 ON DESIGNATIONS2.DES_ID = ACS_KV_STATUS_DES_ID
INNER JOIN DES_TEXTS
AS DES_TEXTS2 ON DES_TEXTS2.TEX_ID = DESIGNATIONS2.DES_TEX_ID
WHERE
ART_ID IN (
52277
) AND
DESIGNATIONS.DES_LNG_ID = 19 AND
DESIGNATIONS2.DES_LNG_ID = 19;
SELECT
DES_TEXTS.TEX_TEXT AS CRITERIA_DES_TEXT,
IFNULL(DES_TEXTS2.TEX_TEXT, ACR_VALUE) AS CRITERIA_VALUE_TEXT
FROM
ARTICLE_CRITERIA
LEFT JOIN DESIGNATIONS
AS DESIGNATIONS2 ON DESIGNATIONS2.DES_ID = ACR_KV_DES_ID
LEFT JOIN DES_TEXTS
AS DES_TEXTS2 ON DES_TEXTS2.TEX_ID = DESIGNATIONS2.DES_TEX_ID
LEFT JOIN CRITERIA ON CRI_ID = ACR_CRI_ID
LEFT JOIN DESIGNATIONS ON DESIGNATIONS.DES_ID = CRI_DES_ID
LEFT JOIN DES_TEXTS ON DES_TEXTS.TEX_ID = DESIGNATIONS.DES_TEX_ID
WHERE
ACR_ART_ID IN (
52277
) AND
(DESIGNATIONS.DES_LNG_ID IS NULL OR DESIGNATIONS.DES_LNG_ID = 19) AND
(DESIGNATIONS2.DES_LNG_ID IS NULL OR DESIGNATIONS2.DES_LNG_ID = 19);
CREATE TABLE IF NOT EXISTS articles ( ART_ID int(11) NOT NULL, ART_ARTICLE_NR varchar(66) NOT NULL, ART_SUP_ID smallint(6) DEFAULT NULL, ART_DES_ID int(11) DEFAULT NULL, ART_COMPLETE_DES_ID int(11) DEFAULT NULL, ART_PACK_SELFSERVICE smallint(6) DEFAULT NULL, ART_MATERIAL_MARK smallint(6) DEFAULT NULL, ART_REPLACEMENT smallint(6) DEFAULT NULL, ART_ACCESSORY smallint(6) DEFAULT NULL, ART_BATCH_SIZE1 int(11) DEFAULT NULL, ART_BATCH_SIZE2 int(11) DEFAULT NULL, PRIMARY KEY (ART_ID), KEY ART_ARTICLE_NR (ART_ARTICLE_NR(10)) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE IF NOT EXISTS article_criteria ( ACR_ART_ID int(11) NOT NULL, ACR_GA_ID int(11) NOT NULL, ACR_SORT smallint(6) NOT NULL, ACR_CRI_ID smallint(6) NOT NULL, ACR_VALUE varchar(60) DEFAULT NULL, ACR_KV_DES_ID int(11) DEFAULT NULL, ACR_DISPLAY smallint(6) DEFAULT NULL, PRIMARY KEY (ACR_ART_ID,ACR_GA_ID,ACR_SORT) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE IF NOT EXISTS art_country_specifics ( ACS_ART_ID int(11) NOT NULL, ACS_PACK_UNIT int(11) DEFAULT NULL, ACS_QUANTITY_PER_UNIT int(11) DEFAULT NULL, ACS_KV_STATUS_DES_ID int(11) DEFAULT NULL, ACS_KV_STATUS varchar(9) DEFAULT NULL, ACS_STATUS_DATE datetime DEFAULT NULL, KEY ACS_ART_ID (ACS_ART_ID) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE IF NOT EXISTS criteria ( CRI_ID smallint(6) NOT NULL, CRI_DES_ID int(11) NOT NULL, CRI_SHORT_DES_ID int(11) DEFAULT NULL, CRI_UNIT_DES_ID int(11) DEFAULT NULL, CRI_TYPE binary(1) NOT NULL, CRI_KT_ID smallint(6) DEFAULT NULL, CRI_IS_INTERVAL smallint(6) DEFAULT NULL, CRI_SUCCESSOR smallint(6) DEFAULT NULL, PRIMARY KEY (CRI_ID) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE IF NOT EXISTS designations ( DES_ID int(11) NOT NULL, DES_LNG_ID smallint(6) NOT NULL, DES_TEX_ID int(11) NOT NULL, PRIMARY KEY (DES_ID,DES_LNG_ID) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE IF NOT EXISTS des_texts ( TEX_ID int(11) NOT NULL, TEX_TEXT text, PRIMARY KEY (TEX_ID) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE IF NOT EXISTS suppliers ( SUP_ID smallint(6) NOT NULL, SUP_BRAND varchar(60) DEFAULT NULL, SUP_SUPPLIER_NR smallint(6) DEFAULT NULL, SUP_COU_ID smallint(6) DEFAULT NULL, SUP_IS_HESS smallint(6) DEFAULT NULL, PRIMARY KEY (SUP_ID) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
I think this might work. Please note that this is not tested (especially as you've neglected to provide us with the layout of your tables), and I've had to make guesses as to how articles is supposed to be correlated to article_criteria.
SELECT Articles.art_article_nr, Suppliers.sup_brand
Article_Designations.text, Country_Specific_Designations.text,
Article_Criteria_Designations.value,
Article_Criteria_Designations.text
FROM Articles
JOIN (SELECT Designations.des_id, Des_Texts.tex_text as text
FROM Designations
JOIN Des_Texts
ON Des_Texts.tex_id = Designations.des_tex_id
AND Designations.des_lng_id = 19) as Article_Designations
ON Article_Designations.des_id = Articles.art_complete_des_id
JOIN Suppliers
ON Suppliers.sup_id = Articles.art_sup_id
JOIN Art_Country_Specific
ON Art_Country_Specific.acs_art_id = Articles.art_id
JOIN (SELECT Designations.des_id, Des_Texts.tex_text as text
FROM Designations
JOIN Des_Texts
ON Des_Texts.tex_id = Designations.des_tex_id
AND Designations.des_lng_id = 19) as Country_Specific_Designations
ON Country_Specific_Designations.des_id = Art_Country_Specific.acs_kv_status_des_id
LEFT JOIN (SELECT Article_Criteria.acr_art_id,
COALESCE(Art_Cri_Designations.text, Article_Criteria.acr_value) as value,
Criteria_Designations.text
FROM Article_Criteria
LEFT JOIN (SELECT Designations.des_id, Des_Texts.tex_text as text
FROM Designations
JOIN Des_Texts
ON Des_Texts.tex_id = Designations.des_tex_id
AND Designations.des_lng_id = 19) as Art_Cri_Designations
ON Art_Cri_Designations.des_id = Article_Criteria.acr_kv_des_id
LEFT JOIN (SELECT Criteria.cri_id, Des_Texts.tex_text as text
FROM Criteria
JOIN Designations
ON Designations.des_id = Criteria.cri_des_id
JOIN Des_Texts
ON Des_Texts.tex_id = Designations.des_tex_id
AND Designations.des_lng_id = 19) as Criteria_Designations
ON Criteria_Designations.cri_id = Article_Criteria.acr_cri_id
) as Article_Criteria_Designations
ON Article_Criteria_Designations.acr_art_id = Articles.art_id
WHERE Articles.art_id IN (52277)
I need a little help for one sql query;
here is my basic user database table
CREATE TABLE IF NOT EXISTS `users` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`email` varchar(100) NOT NULL,
`pass` varchar(255) NOT NULL,
`fname` varchar(50) NOT NULL,
`lname` varchar(40) NOT NULL,
`network` varchar(10) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=27 ;
and here is my basic topics database table
CREATE TABLE IF NOT EXISTS `topics` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`userid` varchar(100) NOT NULL,
`title` varchar(255) NOT NULL,
`body` text,
`tags` varchar(256) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ;
What I'm trying to do is;
i.e. list all topics from users who joined "harvard" network
You just need a simple JOIN. Join the tables on the userID (of those who are in "harvard").
SELECT title,body
FROM topics, users
WHERE userid = users.id
AND network = 'harvard'
Or using the JOIN keyword:
SELECT title,body
FROM topics
JOIN users ON userid = users.id
WHERE network = 'harvard'
This should do the trick, I think:
SELECT
title
FROM
topics t
INNER JOIN
users u ON t.userid=u.id
WHERE
network='Harvard'
SELECT t.title
FROM users u
INNER JOIN topics t ON u.id = t.userid
WHERE u.network = 'harvard'
GROUP BY u.id
I have a users table.
I have a certifications table.
Each user can have multiple certifications. Certifications has a user_id foreign key.
How may I select a user as well as all of their certifications in one query?
How may I select users that have certifications?
users table:
CREATE TABLE `users` (
`id` int(11) unsigned NOT NULL auto_increment,
`username` varchar(24) default NULL,
`displayname` varchar(24) default NULL,
`email` varchar(64) default NULL,
`password` text,
`signup_date` int(11) default NULL,
`signup_ip` varchar(15) default NULL,
`hash` text,
`verified` tinyint(1) default '0',
`last_login` int(11) default NULL,
`logins` int(11) default NULL,
`status` text,
`recovery_hash` text,
`recovery_initiated` int(11) default NULL,
`last_updated` int(11) default NULL,
`signup_method` text,
`signup_question` int(11) default NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
certifications table:
CREATE TABLE `certifications` (
`id` int(11) unsigned NOT NULL auto_increment,
`user_id` int(11) unsigned NOT NULL,
`number` varchar(128) default NULL,
`board` varchar(128) default NULL,
`company` varchar(128) default NULL,
`website` varchar(128) default NULL,
PRIMARY KEY (`id`),
KEY `user_id` (`user_id`),
CONSTRAINT `certifications_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
Thank you for reading.
How may I select a user as well as all of their certifications in one query?
SELECT *
FROM certifivations C
LEFT JOIN users U
ON C.user_id = U.id
WHERE U.id=USERID
How may I select users that have certifications?
SELECT *
FROM certifivations C
JOIN users U
ON C.user_id = U.id
GROUP BY U.id
Get all the certifications of user with id of 1 by using a join.
SELECT b.id as certification_id
FROM users AS a
LEFT JOIN certifications AS b
ON a.id = b.user_id
WHERE a.id = 1;
Get all the users that have certifications using an inner join. All users without certs will drop out.
SELECT a.id as users_with_certifications
FROM users as a
JOIN certifications AS b
ON a.id = b.user_id;