Here's my schema:
Table "Questoes";
+----------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| id_quest | int(11) | NO | | NULL | |
| questao | varchar(255) | NO | | NULL | |
| nivel | int(11) | NO | | NULL | |
| tipo | varchar(255) | NO | | NULL | |
+----------+--------------+------+-----+---------+----------------+
Table "Respostas";
+----------+---------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+---------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| id_quest | int(11) | NO | | NULL | |
| resposta | varchar(255) | NO | | NULL | |
| r_valido | enum('0','1') | NO | | NULL | |
+----------+---------------+------+-----+---------+----------------+
My query is:
SELECT q.questao, r.resposta
FROM questoes q, respostas r
WHERE q.id_quest IN (19,20,21)
AND q.id_quest=r.id_quest
AND r.r_valido = ( SELECT resposta FROM respostas WHERE r_valido= 1 )
What I need is the field questao from table Questoes and the field resposta from table respostas where field r_valido = 1.
The field resposta have 4 results, and only one is valid, in other words, where the field r_valido = 1.
Your query should look like this:
SELECT q.questao, r.resposta FROM questoes AS q JOIN respostas AS r ON r.id_quest = q.id_quest WHERE q.id_quest IN (19,20,21) AND r.r_valido = "1"
Also I found out what is causing that weird error when you use 1 instead of "1" in the query:
We strongly recommend that you do not use numbers as enumeration
values, because it does not save on storage over the appropriate
TINYINT or SMALLINT type, and it is easy to mix up the strings and the
underlying number values (which might not be the same) if you quote
the ENUM values incorrectly
I didn't understood you completely but i think this is what you looking for:
SELECT q.questao, r.resposta
FROM questoes as q
INNER JOIN respostas as r
ON q.id_quest=r.id_quest
WHERE
q.id_quest IN (19,20,21) AND
r.r_valido = '1'
Related
I have 2 tables the first one is the product-page visited
+------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| idproduct | varchar(128) | YES | | NULL | |
| logdate | date | YES | | NULL | |
| idmagasin | int(20) | YES | | NULL | |
| idenseigne | int(20) | YES | | NULL | |
| commanded | int(2) | YES | | 0 | |
+------------+--------------+------+-----+---------+----------------+
And the second one is the product commanded
+-------------+--------------+------+-----+-------------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+--------------+------+-----+-------------------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| idproduct | varchar(255) | NO | | NULL | |
| idenseigne | int(11) | NO | | NULL | |
| idmagasin | int(11) | NO | | NULL | |
| ingredients | tinytext | YES | | NULL | |
| date | timestamp | NO | | CURRENT_TIMESTAMP | |
+-------------+--------------+------+-----+-------------------+----------------+
How can i update the column commanded in product_visited , if product_visited.idproduct = product_commanded.idproduct and product_visited.logdate = product_commanded.date
i'm confused to use inner join or exists
I want to update product_visited.commanded = 1 when the value of logdate and idproduct exists in product_commanded, it will mean the product visited is commanded too
I believe this is what you are looking for:
Update product_visited pv
set commanded = 1
Where exists (Select 1
from product_commanded pc
where pv.idproduct = pc.idproduct and pv.logdate = pc.date
);
Ok, I've made guesses with the join fields but you're after something like this;
UPDATE pv
SET pv.Commanded = 1
FROM Product_Visited pv
JOIN Product_Commanded pc
ON pv.logdate = pc.date
AND pv.idproduct = pc.id
The inner join means that you're only going to update records in Product_Visited where there are matching rows in Product_Commanded based on the join predicates you give it.
Note: this is a SQL Server answer. May or may not work in MySQL
Sounds like you want to update commanded whenever a record exists for same product in commanded table?
in any database:
Update product_visited set commanded = 1
Where exists(Select * from product_commanded
where product_id = product_visited.Product_id)
I am using Entrust's default table structure:
permissions table:
+--------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| name | varchar(255) | NO | UNI | NULL | |
| display_name | varchar(255) | YES | | NULL | |
| description | varchar(255) | YES | | NULL | |
| created_at | timestamp | NO | | NULL | |
| updated_at | timestamp | NO | | NULL | |
+--------------+------------------+------+-----+---------+----------------+
permission_role table:
+---------------+------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------------+------------------+------+-----+---------+-------+
| permission_id | int(10) unsigned | NO | PRI | NULL | |
| role_id | int(10) unsigned | NO | PRI | NULL | |
+---------------+------------------+------+-----+---------+-------+
roles table:
+--------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| name | varchar(255) | NO | UNI | NULL | |
| display_name | varchar(255) | YES | | NULL | |
| description | varchar(255) | YES | | NULL | |
| created_at | timestamp | NO | | NULL | |
| updated_at | timestamp | NO | | NULL | |
+--------------+------------------+------+-----+---------+----------------+
Now, given a role_id I'd like to get select the following from this database:
permissions.id
permissions.display_name
whether the permission_role table contains an entry with the permission_id and the given role_id
The last one turned out to be a bit tricky in Eloquent.
This SQL query accomplishes exactly what I need (ID is obviously replaced by a valid role ID):
SELECT p.id, p.display_name, IF(pr.role_id = ID, 1, 0) AS has_role
FROM permissions p
LEFT OUTER JOIN permission_role pr ON p.id = pr.permission_id;
Example output:
+----+--------------+----------+
| id | display_name | has_role |
+----+--------------+----------+
| 1 | Edit users | 1 |
| 2 | View users | 0 |
| 3 | Delete users | 0 |
+----+--------------+----------+
Can anyone help me out here, on how to do this using Eloquent?
I've tried this, but it always returns 1 (true) in the third column, unlike the SQL query (as seen above).
$result = DB::table('permissions')
->leftJoin('permission_role', 'permission_role.permission_id', '=', 'permission_role.role_id')
->select(DB::raw('permissions.id, permissions.display_name, IF(permission_role.role_id = ID, 1, 0) AS has_role'))
->get();
Ideally, I'd like to do this without using DB::raw, although it is completely fine if that is what it takes.
Thanks in advance for any help!
Structurally, the Query Builder query you've shown looks fine.
What does not look fine is the left join. Shouldn't this:
->leftJoin('permission_role', 'permission_role.permission_id', '=', 'permission_role.role_id')
be this:
->leftJoin('permission_role', 'permission_role.permission_id', '=', 'permissions.id')
?
I have these following tables:
Table "Questoes";
+----------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| id_quest | int(11) | NO | | NULL | |
| questao | varchar(255) | NO | | NULL | |
| nivel | int(11) | NO | | NULL | |
| tipo | varchar(255) | NO | | NULL | |
+----------+--------------+------+-----+---------+----------------+
Table "Resultados";
+-----------+---------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------+---------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| id_user | int(11) | NO | MUL | NULL | |
| nivel | int(11) | NO | | NULL | |
| pontuacao | int(24) | NO | | NULL | |
| data | date | NO | | NULL | |
+-----------+---------+------+-----+---------+----------------+
And table "utilizador";
+-----------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------+--------------+------+-----+---------+----------------+
| id_user | int(11) | NO | PRI | NULL | auto_increment |
| id_tipo | int(11) | NO | | 1 | |
| username | varchar(50) | NO | UNI | NULL | |
| password | varchar(20) | NO | | NULL | |
| nome | varchar(50) | NO | | NULL | |
| email | varchar(100) | NO | | NULL | |
| data_nasc | text | NO | | NULL | |
| e_valido | smallint(6) | NO | | 0 | |
+-----------+--------------+------+-----+---------+----------------+
What i need is the maximum of score (field "pontuacao") of each level (field "nivel") in the table "resultados" to the best player.
To get the players name, will be through the field "id_user" in table "resultados".
I have this, but doesn'st work:
SELECT u.id_user, MAX(u.pontuacao), u.nivel, u.data, l.nivel, r.id_user, r.username
FROM questoes l, resultados u, utilizador r
WHERE u.nivel = l.nivel AND r.id_tipo=1 AND u.id_user=r.id_user
GROUP BY u.nivel
SELECT a.id_user, r.username, b.pontuacao, b.nivel
FROM resultados a
INNER JOIN utilizador r on a.id_user=r.id_user
INNER JOIN (SELECT u.nivel, MAX(u.pontuacao) pontuacao
FROM resultados u
GROUP BY u.nivel) b ON a.nivel = b.nivel AND a.pontuacao = b.pontuacao
It doesn't work because you are relying on an extension to MySQL that doesn't do what you expect. When using group by, just be sure all the columns in the select are either aggregated or in the group by.
What you want is more like:
SELECT u.id_user, r2.pontuacao, u.nivel, u.data, l.nivel, r.id_user, r.username
FROM questoes l join
resultados u
u.nivel = l.nivel join
utilizador r
on u.id_user = r.id_user join
(select r2.nivel, max(r2.pontuacao) as maxp
from resultados r2
group by r2.nivel
) r2
on u.nivel = r2.nivel and u.pontuacao = r2.maxp
WHER r.id_tipo = 1
GROUP BY u.nivel;
This calculates the maximum value as a subquery and then joins it back in. You may not need the group by after this. Also, I fixed the joins so they use proper ANSI syntax.
Suppose I have two tables, one with list of spells and another with a grimory, the list of spells that a user has selected for learn or already learned.
mysql> SHOW COLUMNS FROM Grimory;
+--------------+------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| personage_id | int(11) | YES | MUL | NULL | |
| spell_id | int(11) | YES | MUL | NULL | |
| isLearned | tinyint(1) | NO | | NULL | |
| isSelected | tinyint(1) | NO | | NULL | |
+--------------+------------+------+-----+---------+----------------+
5 rows in set (0.00 sec)
mysql> SHOW COLUMNS FROM Spell;
+-----------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(255) | NO | | NULL | |
| description | longtext | NO | | NULL | |
| chatDescription | longtext | NO | | NULL | |
| level | int(11) | NO | | NULL | |
| isActive | tinyint(1) | NO | | NULL | |
| category_id | int(11) | YES | MUL | NULL | |
| createdAt | datetime | YES | | NULL | |
+-----------------+--------------+------+-----+---------+----------------+
8 rows in set (0.00 sec)
The problem.
I want to display a list with ALL spells by category_id, BUT for every row I want to show if that spell is learned or selected by current user (personage_id).
Can you help me to write a proper query?
How this works:
With php I generate: List of spells | checkbox isSelected | checkBox isStudied
When I click on isSelected checkbox, a record in grimory will be added with current user and spell.
Updated
SELECT a.*, IFNULL(b.isLearned,0) as isLearned,
IFNULL(b.isSelected,0) as isSelected
FROM Spell a
LEFT JOIN Grimory b(ON b.spell_id =a.id
AND b.personage_id =:current_user_id)
WHERE a.category_id = :current_category_id
SELECT * FROM Spell
INNER JOIN Grimory
ON Spell.id = Grimory.spell_id
WHERE (Grimory.isLearned = 1 OR Grimory.isSelected = 1)
AND Spell.category_id = 'YOUR CAT ID IS HERE'
I have a content table that can have varying result lengths of comments and tags associated with each row. I am unsure of how to get all of this information for a piece of content in one swoop.
I can easily make a query to get the content info (id) and query each table for the comments and tags but that doesn't seem like it makes much sense as if there is a better way.
Any tips or suggestions?
Thanks in advance!
Thomas
My query SO far looks like:
SELECT c.*, GROUP_CONCAT(tagWords.tagWord SEPARATOR ', ') AS tags FROM platform.contents c
LEFT OUTER JOIN platform.contentTags ON contentTags.contentId = c.contentId
LEFT OUTER JOIN platform.tagWords ON contentTags.tagId = tagWords.tagId
WHERE c.contentType = 'album' LIMIT 10
GROUP_CONCAT is great and all but I need the fields from each table and as the tables will grow. :/
My table layouts:
mysql> explain contentComments;
+--------------+---------------------+------+-----+-------------------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------------+---------------------+------+-----+-------------------+-------+
| contentId | bigint(19) unsigned | NO | MUL | NULL | |
| userId | bigint(19) unsigned | NO | | NULL | |
| message | varchar(255) | YES | | NULL | |
| stampCreated | timestamp | NO | | CURRENT_TIMESTAMP | |
+--------------+---------------------+------+-----+-------------------+-------+
4 rows in set (0.00 sec)
mysql> explain contentTags;
+-----------+------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+------------+------+-----+---------+-------+
| contentId | bigint(20) | NO | MUL | NULL | |
| tagId | bigint(20) | YES | | NULL | |
+-----------+------------+------+-----+---------+-------+
2 rows in set (0.00 sec)
mysql> explain contents;
+------------------+-----------------------------------------+------+-----+-------------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------------+-----------------------------------------+------+-----+-------------------+----------------+
| contentId | bigint(20) | NO | PRI | NULL | auto_increment |
| contentType | enum('video','album','blogpost','news') | NO | | NULL | |
| userId | bigint(19) unsigned | NO | MUL | NULL | |
| contentTitle | varchar(45) | YES | | NULL | |
| contentDesc | varchar(255) | YES | | NULL | |
+------------------+-----------------------------------------+------+-----+-------------------+----------------+
mysql> explain tagWords;
+---------+---------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------+---------------------+------+-----+---------+----------------+
| tagId | bigint(19) unsigned | NO | PRI | NULL | auto_increment |
| tagWord | varchar(45) | YES | | NULL | |
+---------+---------------------+------+-----+---------+----------------+
2 rows in set (0.00 sec)
That looks right to me, though I think you also need a group_concat for comments. It could be that your brain isnt comfortable with the idea of N to N relationships in a database, which would be strange considering it looks to me like you did it right :p