Does MySQL table row contain two values - mysql

I have a table with two columns:
ID1 | ID2
---------
1 | A
3 | V
1 | C
4 | B
5 | Q
1 | S
And I want to be able to find out if any row has, say ID1 = 5 and ID2 = Q , and return a true / false value.

Yes, Of course
SELECT * FROM table where ID1='5' and ID2='Q'
PHP (I am just guessing this backend)
$query = "SELECT * FROM table where ID1='5' and ID2='Q'";
$result = mysql_query($query);
if(mysql_num_rows($result)) { return true; }
else { return false; }

1 means TRUE for mysql
SELECT 1
FROM your_table
WHERE ID1 = 5 AND ID2 = 'Q'

for example this?
SELECT 1
FROM TABLE
WHERE ID1 = 5 AND ID2 = 'Q'

Efficient query for your purpose (faster than other examples):
SELECT 1 FROM table where ID1='5' and ID2='Q' LIMIT 1
PHP sample:
<?php
$query = "SELECT 1 FROM table where ID1='5' and ID2='Q' LIMIT 1";
echo bool(mysql_num_row(mysql_query($query)));
?>

Related

How to query CodeIgniter in two table with condition?

I got headache of this query issue in CodeIgniter
I'm a beginner of this framework
How to query in CodeIgniter of Query result in Table 1 and Table 2.
If you are using the Query Builder in Codeigniter version 3 (I assume since your using the tag codeigniter-3), then you could specify the columns in your select statement and use join to achieve what you want:
$this->db->select('t1.Account, t1.Client, t1.Wala, t2.Meron');
$this->db->from('Table1 t1');
$this->db->join('Table2 t2', 't1.Account = t2.Account AND t1.Client = t2.Client');
$this->db->where('t1.Account', 111); // IF YOU ONLY WANT A SPECIFIC ACCOUNT
$this->db->group_by('t1.Account, t1.Client');
$query = $this->db->get();
if ($query->num_rows() > 0){
$result = $query->result(); // ARRAY OF OBJECTS
foreach ($result as $r){
echo $r->Account."\t";
echo $r->Client."\t";
echo $r->Wala."\t";
echo $r->Meron."\n";
}
}
The result:
| Account | Client | Wala | Meron |
|---------|--------|------|-------|
| 111 | Doe | | 50 |
| 111 | Jhon | 200 | 50 |
Documentation: https://www.codeigniter.com/userguide3/database/query_builder.html
Alternatively, you can use a pure SQL query to get the same result:
$sql = 'SELECT t1.Account, t1.Client, t1.Wala, t2.Meron
FROM Table1 t1
JOIN Table2 t2
ON t1.Account = t2.Account AND t1.Client = t2.Client
WHERE t1.Account = 111
GROUP BY t1.Account, t1.Client';
$query = $this->db->query($sql);
If you are using MySQL > 5.6 then use the group by function any_value():
t1.Account, t1.Client, ANY_VALUE(t1.Wala), ANY_VALUE(t2.Meron)
To be used with caution since unexpected results may appear if you have multiple rows with different values in the columns "Wala" or "Meron" for the same Account and Client.

How to query rows from 2 tables in a single query?

TABLE 1
student_id | name
-----------------
1 | A
2 | B
3 | C
4 | D
TABLE 2
vote_id | student_id | vote_for
------------------------------
1 | 1 | 2
2 | 1 | 3
3 | 2 | 1
4 | 1 | 4
How can I get records from TABLE 1 (student names B C D) based on student_id in TABLE 2 (student_id 1) in a single query? I managed to do it but in multiple queries like below:
$students = array();
$query = "SELECT vote_for FROM table2 WHERE student_id=?";
$stmt = $this->con->prepare($query);
$stmt->bind_param("i",$student_id);
$stmt->execute();
$stmt->bind_result($vote_for);
$votes = array();
while($stmt->fetch()){
$votes[] = $vote_for;
}
$stmt->close();
if (!empty($votes)) {
$query = "SELECT name FROM table1 WHERE student_id=?";
foreach ($votes as $vote) {
$stmt = $this->con->prepare($query);
$stmt->bind_param("i",$vote);
$stmt->execute();
$stmt->bind_result($name);
while($stmt->fetch()){
$temp = array();
$temp['name'] = $name;
$students[] = $temp;
}
$stmt->close();
}
}
You can use a JOIN query to get the names of the students that were voted for by a given student_id. For example:
SELECT s.name AS voted_for
FROM table2 v
JOIN table1 s ON s.student_id = v.vote_for
WHERE v.student_id = 1
Demo on dbfiddle
In PHP:
$students = array();
$query = "SELECT s.name AS voted_for
FROM table2 v
JOIN table1 s ON s.student_id = v.vote_for
WHERE v.student_id = ?";
$stmt = $this->con->prepare($query);
$stmt->bind_param("i",$student_id);
$stmt->execute();
$stmt->bind_result($name);
while($stmt->fetch()) {
$students[] = array('name' => $name);
}
$stmt->close();
I believe you can achieve it with the following query:
SELECT T1.STUDENT_ID, T1.NAME
FROM TABLE_1 T1, TABLE_2 T2
WHERE T1.STUDENT_ID = T2.VOTE_FOR
AND T2.STUDENT_ID = ?
And you would just inject the STUDENT_ID for the Table 2. You can remove the T1.STUDENT_ID if necessary.

Sum of two queries

My table looks like this:
I made two queries:
SELECT COUNT(column_1) AS result_1 FROM table WHERE column_1= 5;
SELECT COUNT(column_2) AS result_2 FROM table WHERE column_2 = 5;
The first query gives me result_1 = 2, and the second result_2 = 3
I need to make a query that will give me the sum of these two (2+3=5).
I always get two results or just one of them.
Each of these expressions:
column_1 = 5
and
column_2 = 5
evaluates to 0 or 1 (false or true).
So you can use them like this:
SELECT SUM(column_1 = 5) + SUM(column_2 = 5) AS result FROM table
See the demo.
Result:
| result |
| ------ |
| 5 |
You can use subqueries:
SELECT sub1.result_1 + sub2.result_2 FROM (
SELECT COUNT(column_1) AS result_1 FROM table WHERE column_1 = 5) as sub1, (
SELECT COUNT(column_2) AS result_2 FROM table WHERE column_2 = 5) as sub2

Combine two SELECT queries together by using value from SELECT in WHERE

id | number
---|--------
1 | null
2 | 1
3 | null
4 | 0
5 | 4
6 | 4
This is what I want to do this in one query. How can I combine both of these queries together?
$id = 4;
SELECT * FROM users WHERE id = $id OR number = $id
// This will return rows 4,5,6
2nd query:
$id = 5;
SELECT * FROM users WHERE id = $id AND id = $numberColumnOfId AND number = $numberOfColumnId;
// This will return rows 4,5,6
$numberColumnOfId is the number of the $id. So if the $id = 5, then the number would be 4. So select rows where ID = 5.
How can I combine both of the SELECT statements together?
Use two queries that you combine with UNION.
The first query gets the rows with the given $id, the second query gets the ones that match via thenumber` column.
SELECT *
FROM users
WHERE id = $id OR number = $id
UNION
SELECT u1.*
FROM users AS u1
JOIN users AS u2 ON u1.number = u2.number OR u1.id = u2.number
WHERE u2.id = $id or u2.number = $id
DEMO

select record which is not present in other table in codeigniter

i have used the following query to select rows from tables.
Table 1:
id description status add_date topicid
1 xyz 0 22-3-13 5
2 pqr 0 21-3-13 5
3 abc 0 20-3-13 5
4 sdd 0 22-3-13 5
Table2:
id otherid
1 2
2 3
This query gives me all the record from table1 but i want to select those record which is not in table2.
like table1 'id' not present in table2 'otherid'.
In my case want to select record from table1 for id 1 and 4.because which is not present in table2 as 'otherid'.
$topicid = 5;
$q =$this->db->select(array(
't1.id as id',
't1.description',
't1.topicid',
't1.add_date'))
->from('table1 AS t1')
->where('t1.topicid',$topicid)
->where('t1.status',0)
->order_by('t1.add_date DESC)->get();
try this query will work
$topicid = 5;
$q =$this->db->select(array(
't1.id as id',
't1.description',
't1.topicid',
't1.add_date'))
->from('table1 AS t1')
->where('t1.topicid',$topicid)
->where('t1.status',0)
->where('t1.id NOT IN (select otherid from table2)',NULL,FALSE)
->order_by('t1.add_date DESC)->get();
$select = array(
't1.id as id',
't1.description',
't1.topicid',
't1.add_date'
);
$this->db
->select($select)
->join('Table2','Table2.otherid = Table1.id','left')
->where('Table2.otherid IS','NULL')
->where('Table1.topicid',$topicid)
->where('Table1.status',0)
->get('Table1');