I have two tables in my database announcements and announcements_external:
+----------+ +----------+
| table1 | | table2 |
+----------+ +----------+
| id | | id |
| catid | | eid |
| uid | | username |
| title | | source |
| text | | title |
| views | | section |
| images | | category |
| created | | text |
| modified | | views |
| pubdate | | images |
+----------+ | created |
| modified |
+----------+
In CodeIgniter framework, I want to merge them, so when I will do a foreach($results as $item), I would output them side by side, ordered by field created.
I tried this code but I know it's completly wrong with the merging:
public function get_mixed( $limit = NULL, $offset = NULL )
{
$this->db
->select('
announcements.id,
announcements.catid,
announcements.uid,
section.title AS section,
categories.title AS category,
announcements.title,
announcements.text,
announcements.views,
announcements.created,
announcements.modified,
announcements.pubdate,
announcements_external.id as eid,
announcements_external.eid,
announcements_external.source,
announcements_external.username,
announcements_external.section as esection,
announcements_external.category as ecategory,
announcements_external.title as etitle,
')
->join('categories', 'announcements.catid = categories.id', 'left')
->join('categories as section', 'categories.pid = section.id', 'left')
->join('announcements_external', 'announcements_external.id = announcements_external.id', 'right')
->order_by('created DESC, '.$this->_order_by);
$query = $this->db->get( $this->_table_name, $limit, $offset )->result();
return $query;
}
By the way, I do LEFT JOIN on categories table, which works as it should, but I thought it would be better to leave it in this code so you will see it also.
Any solution or maybe guide where I can get more information pls ? Thanks
It seems that Table1 and Table2 are separated entities so you need to use UNION ALL instead of JOIN to merge them.
(SELECT ..needed columns .. , Table1.created FROM Table1 JOIN categories ... )
UNION All
(SELECT ..needed columns .. , Table2.created FROM Table2 JOIN categories ... )
ORDER BY created ;
You will need to use fake columns aliases for nonidentical columns, if necessary.
Related
I am using CodeIgniter. I have an employee table and records are
id |firstname | lastname | mobileno | created_by
2 |mnb | nbgfv | 1452145625 | 1
3 |jhg | uhgf | 1452365478 | 2
4 |poi | ijuy | 1458745632 | 2
5 |tgf | tgfd | 1458745254 | 2
6 |wer | qwes | 1523654512 | 2
Now My issue is in the column created_by. When I am displaying the record of any id value then I am getting the output like
id |firstname | lastname | mobileno | created_by
3 |jhg | uhgf | 1452365478 | 2
But my expected output
id |firstname | lastname | mobileno | created_by
3 |jhg | uhgf | 1452365478 | mnb nbgfv
I have to display the name of created_by
I tried only this query.
$get_single_emp_record = array('id' => 3);
$this->db->where($get_single_emp_record);
$query = $this->db->get('tbl_employee');
$result = $query->row();
if($result)
{
return $result;
}
else
{
return 0;
}
I have a hint (maybe this can give solution in your problem).
Try query like this :
SELECT
t1.id , t1.firstname , t1.lastname ,t1.mobileno,
CONCAT(t2.firstname ," ",t2.lastname ) AS createby
FROM tbl_employee AS t1
JOIN tbl_employee AS t2 ON t2.id = t1.created_by
WHERE t1.id = '3'
With above query you no need create temporary table or other additional tables.
I Tested it. >>>
http://sqlfiddle.com/#!9/e693cf/2/0
Thanks
you will have to create another table maybe tbl_creator which will have the id, creator_name then in your query you will perform a Join operation
I have the following three tables
+----------+ +--------+ +-------------+
| adv | | member | | removed_adv |
+==========+ +========+ +=============+
| id | | id | | id |
| group | | group | | adv_id |
| category | | email | | member_id |
| title | | pw | +-------------+
| path | +--------+
| duration |
+----------+
What I am trying to do is to get all the data from table adv, where the member table's group is equal to the adv table's group. If the member table's id is equal to the _removed_adv_ table's _member_id_, that data should be removed from the result set.
I am using CodeIgniter's Active Record.
Could someone please help me? I really don't know how to do this.
You can try this:
select * from adv join member
on adv.group=member.group
where member.id not in (select member_id from removed_adv)
For CodeIgniter:
$removed = // get removed records member_id to here;
$CI->db->select('ad');
$CI->db->from('adv ad');
$CI->db->join('mem m', 'ad.group=mem.group', 'left');
$CI->db->where_not_in('mem.id', $removed);
$query = $CI->db->get();
I have two different tables that share an 'is_featured' column. I need to select all content that is checked 'is_featured' and sort the results by 'date_display' desc.
PHOTOS
item_name | date_display | is_featured
photo1 | 01-02-13 | yes
photo2 | 02-12-13 | yes
photo6 | 06-24-12 | no
photo23 | 09-24-12 | no
VIDEOS
item_name | date_display | is_featured
video5 | 01-14-13 | no
video10 | 03-09-13 | no
video30 | 03-21-13 | yes
video17 | 11-14-12 | yes
DESIRED RESULTS - All FEATURED CONTENT Sorted by Date DESC
item_name | date_display | is_featured
video30 | 03-21-13 | yes
photo2 | 02-12-13 | yes
photo1 | 01-02-13 | yes
video17 | 11-14-12 | yes
I'm able to get my desired result with this UNION query:
SELECT *
FROM (SELECT item_name, date_display
FROM photos
WHERE is_featured='yes'
UNION
SELECT item_name, date_display
FROM videos
WHERE is_featured='yes'
) x
ORDER BY date_display DESC
What I need now is to be able to add unique identifiers to the item_names of each table. For examples: photos.item_name and videos.item_name.
Is that possible with the query I have?
I ultimately need to do something like the following:
If (item_name is from Photos table) { do this }
Try something like:
...
FROM (SELECT item_name, date_display, 'photo' AS media_type
FROM photos...
and similarly for the video subquery. This will create a column media_type in your result set that is 'photo' for the photos, and 'video' for the videos.
Your result set would look like this:
item_name | date_display | media_type
video30 | 03-21-13 | video
photo2 | 02-12-13 | photo
photo1 | 01-02-13 | photo
video17 | 11-14-12 | video
Exactly how you check that value depends on how you handle your query results, but here's some Code Igniter-like pseudo-code provided for concreteness. You can adapt it to whatever your situation calls for:
foreach($query->result() as $row) {
if ($row->media_type == 'photo') {
$featured_photos[$row->item_name] = $row;
}
else {
$featured_videos[$row->item_name] = $row;
}
}
That example is a little silly because it just undoes the work of the union in your query, but the point is that once you get your results back you can use the media_type field to differentiate where the items came from.
I have 2 tables in MySQL:
Persons
|ID | Name | Address
---------------------------------
| 1 | Someone | Somewhere
| 2 | Person2 | Somewhere else
And the table ActivePages
|ID | PersonID | Page
---------------------------------------
| 1 | 1 | somepage
| 1 | 1 | someotherpage
Now I need a query to return the following:
|PersonID | Count
------------------
|1 | 2
|2 | 0
------------------
Who can help me with this? I think its straight forward but I keep getting wrong values...
Thanks in advance!
SELECT p.ID AS PersonID,
COUNT(a.PersonID) AS `Count`
FROM Persons p
LEFT JOIN ActivePages a ON a.PersonID = p.ID
GROUP BY p.ID
select personid,count(personid) as count from activepages
right join Persons on ActivePages.PersonID = Persons.ID group by personid
I have a blog-style app that allows users to tag each post with topics. I keep this data in three separate tables: posts (for the actual blog posts), topics (for the various tags), and posts_topics (a table that stores the relationships between the two).
In order to keep the MVC structure (I'm using Codeigniter) as clean as possible, I'd like to run one MySQL query that grabs all the post data and associated topic data and returns it in one array or object. So far, I'm not having any luck.
The table structure is like this:
Posts
+--------+---------------+------------+-----------+--------------+---------------+
|post_id | post_user_id | post_title | post_body | post_created | post_modified |
+--------+---------------+------------+-----------+--------------+---------------+
| 1 | 1 | Post 1 | Body 1 | 00-00-00 | 00-00-00 |
| 2 | 1 | Post 1 | Body 1 | 00-00-00 | 00-00-00 |
+--------+---------------+------------+-----------+--------------+---------------+
// this table governs relationships between posts and topics
Posts_topics
+--------------+---------------------+-------------------------+-----------------+
|post_topic_id | post_topic_post_id | post_topic_topic_id | post_topic_created |
+--------------+---------------------+-------------------------+-----------------+
| 1 | 1 | 1 | 00-00-00 |
| 2 | 1 | 2 | 00-00-00 |
| 3 | 2 | 2 | 00-00-00 |
| 4 | 2 | 3 | 00-00-00 |
+--------------+---------------------+-------------------------+-----------------+
Topics
+---------+-------------+-----------+----------------+
|topic_id | topic_name | topic_num | topic_modified |
+---------+-------------+-----------+----------------+
| 1 | Politics | 1 | 00-00-00 |
| 2 | Religion | 2 | 00-00-00 |
| 3 | Sports | 1 | 00-00-00 |
+---------+-------------+-----------+----------------+
I have tried this simple query with n success:
select * from posts as p inner join posts_topics as pt on pt.post_topic_post_id = post_id join topics as t on t.topic_id = pt.post_topic_topic id
I've also tried using GROUP_CONCAT, but that gives me two problems: 1) I need all the fields from Topics, not just the names, and 2) I have a glitch in my MySQL so all GROUP_CONCAT data is returned as a BLOB (see here).
I'm also open to hearing any suggestions where I run two queries and try to build out an array for each result; I tried that with the code below but failed (this code also includes joining the user table, which would be great to keep that as well):
$this->db->select('u.username,u.id,s.*');
$this->db->from('posts as p');
$this->db->join('users as u', 'u.id = s.post_user_id');
$this->db->order_by('post_modified', 'desc');
$query = $this->db->get();
if($query->num_rows() > 0)
{
$posts = $query->result_array();
foreach($posts as $p)
{
$this->db->from('posts_topics as p');
$this->db->join('topics as t','t.topic_id = p.post_topic_topic_id');
$this->db->where('p.post_topic_post_id',$p['post_id']);
$this->db->order_by('t.topic_name','asc');
$query = $this->db->get();
if($query->num_rows() > 0)
{
foreach($query->result_array() as $t)
{
$p['topics'][$t['topic_name']] = $t;
}
}
}
return $posts;
}
Any help greatly appreciated.
This query should do the trick. Just change the * to the field list you desire so you are not pulling excess data every time you run the query.
Select
*
FROM
posts,
post_topics,
topics
WHERE
post_topic_topic_id = topic_id AND
post_topic_post_id = post_id
ORDER BY
post_id, topic_id;
Select
*
FROM
posts,
post_topics,
topics,
users
WHERE
post_topic_topic_id = topic_id AND
post_topic_post_id = post_id AND
post_user_id = user_id
ORDER BY
post_id, topic_id;
Holy Cow, You Can do it! See it helps to help. Never knew, try this
Select
post_id,
GROUP_CONCAT(DISTINCT topic_name) as names
FROM
posts,
post_topics,
topics
WHERE
post_topic_topic_id = topic_id AND
post_topic_post_id = post_id
GROUP BY
post_id;
You get
1, 'politics,relligion'
2, 'sports,relligion'